Tutorial Details
- Technology: iPhone SDK
- Difficulty: Beginner
- Completion Time: 10 - 15 Minutes
The iPhone Maps application is a revolutionary program that combines the power of GPS and Google Maps with an incredibly user-friendly interface. This Quick Tip will teach you how to begin taking advantage of this functionality by launching and configuring the Maps app from within your iPhone SDK project.
Step 1. Take a Trip to Paris
Launching the Maps application within your own app can be done with just the following two lines of code:
UIApplication *app = [UIApplication sharedApplication]; [app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=Paris"]];
On line one, we request a reference to the currently running application instance from the UIApplication class.
On line two, we tell the application to open the supplied URL, which in this case is a valid Google Maps URL. Rather than open the map in Mobile Safari as you might expect, the application instance is smart enough to realize that this request is better handled by the Maps app, and will launch it instead.
Note: Unfortunately, the iPhone Simulator will not open the Maps application with the above code (the Simulator does not have the Maps app). You will need to run this snippet on an actual iPhone OS device to test. Running this code in the iPhone Simulator will launch Google Maps in Mobile Safari.
Voila! Welcome to France.
Step 2. Configure Additional Parameters
Now that we understand the basics, let’s dig a bit deeper into the functionality offered by the Maps API.
The URL we supplied above is formatted as an HTTP GET post to the Google Maps application. It submits the parameter “q” set to the value of “Paris.” The “q” stands for query and it is the most general request type that we can make. A few other commonly used parameters include:
- ll=: Stands for “latittude/longitude” and allows the user much greater accuracy when pulling up maps. This parameter is often used in conjunction with the on-board GPS to plot a “You are Here” point display. The value for this parameter must be supplied in decimal format and must be comma separated.
- saddr=: The start, or “source,” address to use when generating driving directions.
- daddr=: The end, or “destination,” address to use when generating driving directions.
- t=: The type of map that will be displayed.
- z=: The zoom level of the map that will be displayed.
As you may have guessed, you can combine multiple parameters in one GET request. This is done by joining them with the ‘&’ symbol.
You can find more detailed information on supported parameters (with examples) from the official Apple Inc. documentation here.
Step 3. Send Your User to the Eiffel Tower
Let’s conclude with an example of how to combine some of the custom parameters above and send our users to a satellite view of the Eiffel Tower.
The Eiffel Tower is located at Latitude 48° 51’ 32” North, Longitude 002° 17’ 45” East. However, this information is of little use to us in the current format. We need to convert the Degree/Minute/Second format into the decimal format required by the Google Maps ll= parameter.
There are many tools available for quickly doing this online, but, when working with Google maps, I like to use a simple trick to generate a latitude/longitude pair from the map. After you have the location you’re interested in centered on the screen, insert the following code snippet (taken from here) into your browser address bar and press enter:
javascript:void(prompt('',gApplication.getMap().getCenter()));
We’re left with the decimal format of latitude 48.85812229675187 and longitude 2.294490337371826.
Now, to send our user directly to the Eiffel Tower, we could paste in the following:
[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?ll=48.85812229675187,2.294490337371826"]];
By default, this will launch with the graphical map type. This isn’t nearly as impressive as seeing the real thing, so let’s change the map type to satellite by appending ‘&t=k’ to our query string:
[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?ll=48.85812229675187,2.294490337371826&t=k"]];
This works, but our view from the top is just a bit too close. Let’s step out a little bit by setting the map zoom level with ‘&z=19’:
[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?ll=48.85812229675187,2.294490337371826&t=k&z=19"]];
Perfect. La tour eiffel in the palm of our hand.


RoyalSlider – Touch-Enable ... only $12.00 
This is great, Im slowly getting started with the sdk!
Thank you!
Glad to hear the tutorial helped! Let us know if you have any specific SDK topic requests that would help you get started.
Hi Mark !!!
Nice tutorial !!
I would like to see an tutorial about how to work with data storage using plist or Sqlite.
Fábio,
Thanks! Glad you liked it.
We actually have a tutorial on Core Data coming out soon, but I agree with you on plist usage and Sqlite being great topics to cover as well. I'll see what I can do about getting these into the editorial calendar soon!
Best,
Mark H.
Perhaps an SDK round-up similar to other tuts+ websites, perhaps a compendium of resources (both web and print) for each platform for beginners (absolute novices like me with next to no programming knowledge)?
The tutorials seem well formatted, and having had a go of the fortune cookie one (for iPhone), Mobile Tuts+ is definitely one of my top resources (or at least will be once more material is made) alongside Apple's own documentation.
Keep up the excellent work!
@MWUK,
Good ideas, thanks for sharing!
What topics would you like covered in an SDK roundup? Can you provide a link to one of the similar articles you mentioned? I'm definitely interested in publishing this, and will try to work in a brainstorming session on how to approach it. Any feedback anyone would like to offer is welcome.
The resource compendium is also a good idea for a short article to point people in the right direction. I'll look into what I can do about that.
Best,
Mark H.
Hi,
Your tutorials are helping me a lot.
I have a tut suggestion: What about a RSS reader for a blog parsing the feed to a XML?
Congrats for the tutorials. Nice job. :)
Hey Ivan,
Thanks, glad the tuts are helping you learn!
I remember repeatedly banging my head against the wall when I first tried grabbing an XML feed with Cocoa-Touch, so, if Mobiletuts+ can help others avoid that experience, it definitely seems worthwhile. I'll have to try and figure out the best time to fit something like this into our editorial calendar, but it is definitely on my radar now. Thanks again!
Best,
Mark Hammonds
UPDATE:
Just spoke with a contributor who has committed to writing an article on integrating an RSS feed with an iPhone app. I'm still working with him to determine a publication date, but hopefully we can get this out in the next few weeks.
-M.H.
I will wait impatiently for this RSS post. I am trying to make a RSS feed app for a friend’s blog.
I have developed the main view with the last title posts but i dont know how to get the full article (with images and text-formatted) when you “touch” the title in the TableView.
Thanks for replying. :)
Thanks for sharing this great tutorial!
Welcome, thanks for reading!
@Mark
I think the articles that would be the most useful and popular would be ones that help people transition from PHP to Objective-C. Since many people will find this site through Nettuts or Themeforest, guides helping people transition from their current language (PHP) to a language that appears very, very alien (Obj-C) will be quite useful.
My 2 cents.
@Alec,
I couldn't agree more. We have a series on Objective-C commissioned and set for publications within the next few weeks.
I really like your idea of contrasting the languages, such as "Objective-C for PHP developers" or something along those lines. Even though I had prior knowledge and experience with PHP, Perl, JavaScript, etc., I still remember an overwhelming feeling of confusion/frustration when I first dove into the SDK beta and Objective-C. The metaphor I like to use is that it's like going from a working knowledge of an Indo-European language, like German or English, to trying to speak in a Sino-Tibetan language, like Mandarin or Thai. For most web developers, they feel like completely different families because, in fact, they are. On top of the pre-compiled vs. scripting language comparison, add in the fact that Objective-C relies so heavily on advanced OO design patterns (like the Factory method), and even experienced developers in ANSI C99 may have trouble picking it up (I was also in this camp, having started much of my programming experience working with C).
I will look into trying to either supplementing our currently commissioned series with web code examples, or possibly write an "Objective-C for PHP developers" crash course article. In addition, what do you think about the possibility of posting several quick tips just comparing Objective-C syntax with other common languages? So, perhaps a quick tip on writing a file to disk, but comparing line-by-line Objective-C code with PHP, Perl, and, say, Ruby? Let me know if you have any thoughts on this.
Cheers,
Mark Hammonds
@Mark
Exactly.
Obj-C is just so different that you need a combination of line-by-line comparisons. The problem is that on the iPhone SDK, you shouldn’t be write and opening text files, you should be use Core Data or sqlite or something. You can’t just do a “echo ‘Hello World!’” kind of thing either because their is no console, it is all user interface. It would take some thought in how to construct it so that the paradigm shift makes sense.
As I work through the iPhone SDK myself, I will want to write tutorial(s) for people who are familiar with advanced PHP techniques but are confused by the complexities of Obj-C.
I would probably compare Obj-C with PHP, Javascript and Ruby, because I think people are much more familiar with Javascript than Perl and Javascript has more of the object interactions stuff–which PHP and Ruby don’t have.
(sorry for the delay, I went to 4 successive lacrosse games! )
Just for the record, it’s “La tour eiffel” ;), nice tutorial btw
Ah, thanks for the catch! The text has been corrected.
Nice read. i recently begun also learning how to code Obj-C. Well ya its rough! I am a Flash Developer and wanted to know if the transition will be steep? ( I do have PHP background though)
thanks
The transition from a scripting language like ActionScript or PHP to a compiled language like Objective-C is steep for most developers, but that does depend on how strong your background in Computer Science and Object Oriented Programming (OOP) / Design (OOD) is as well. If you’re very new to OO, expect a very steep learning curve. The good news is that there are a lot of great resources out there to help you, this site included!
url opens in mobile safari (on iphone 3gs, build base/target iOS 4.1)
any suggestions/info/help why this happens, and how to “fix” it?
regards,
klemens
Hey Klemens,
This post was originally written with SDK 3, but I just double checked on 4.1 and it still works fine for me. An important thing to note though is that you won’t be able to make this work in the simulator -you’ll need to actually test it out on your device.
I’m sending you an e-mail to the account you have registered on our site. If you’re still running into problems testing on a physical device, send me your project code and I’ll take a look.
Cheers,
Mark Hammonds
thx for your fast answer.
it’s not working in simulator, i know. but for some reason it doesn’t work on my iphone when I Build&Run on the iphone (as mentioned build base and target are iOS 4.1).
here is the code snippet:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
switch (tapCount) {
case 1: {
if (touch.view == map_btn) {
map_btn.backgroundColor = [UIColor whiteColor];
NSLog(@”map_btn”);
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?g=Paris"]];
} else if (touch.view == oh_btn) {
NSLog(@”oh_btn”);
oh_btn.backgroundColor = [UIColor whiteColor];
} else if (touch.view == gallery_btn) {
NSLog(@”gallery_btn”);
gallery_btn.backgroundColor = [UIColor whiteColor];
}
}
break;
}
}
don’t know how the q param turned into a g (did copy-paste), but feel like a giant douche right now:
- http://maps.google.com/maps?g=Paris
+ http://maps.google.com/maps?q=Paris
thx anyway.
this helps:
http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html
Although the Maps application supports many of the Google Maps parameters and queries, it does not support them all. The rules for creating a valid map link are as follows:
The domain must be google.com and the subdomain must be maps or ditu.
The path must be /, /maps, /local, or /m if the query contains site as the key and local as the value.
The path cannot be /maps/*.
All parameters must be supported. See Table 1 for list of supported parameters.
A parameter cannot be q=* if the value is a URL (so KML is not picked up).
The parameters cannot include view=text or dirflg=r.
Ah, yeah, it’s easy to overlook a bug like that one. You’re right about the parameter support as well. If you look under “Step 2″ in the tut, I actually link to the same Apple doc you reference for the format rules.
Anyway, glad you got the code working!
Cheers,
Mark Hammonds
I’m just starting with xcode, so I have very little experience. This seems like a great tutorial, but I’m having troubles. I created a windows based project, and pasted the two lines of code you supplied into the implementation file. When I ran the application, I got the following two build errors:
error: initializer element is not constant
error: expected identifier or ‘(‘ before ‘[‘ token
Do I need to add something else to the code in order for the application to run? Thanks in advance!
Congratulations for this tutorial Mark!
iPhone SDK is real a challenge for developers.
many thanks to your wonderful tuts!!! it helps me!!! im starting jumping to a mobile programming iOS.
Thanks for sharing !
Very useful information.
If you want to quickly build an iOS app with Map and Chat integrated, download our ‘supersample’ here as .zip or check out from GIT:
http://quickblox.com/2011/12/supersample-ios/
Nice tutorial! Simple things like this are perfect!