70

The Apple Developer Documentation (link is dead now) explains that if you place a link in a web page and then click it whilst using Mobile Safari on the iPhone, the Google Maps application that is provided as standard with the iPhone will launch.

How can I launch the same Google Maps application with a specific address from within my own native iPhone application (i.e. not a web page through Mobile Safari) in the same way that tapping an address in Contacts launches the map?

NOTE: THIS ONLY WORKS ON THE DEVICE ITSELF. NOT IN THE SIMULATOR.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
davidmytton
  • 38,604
  • 37
  • 87
  • 93

16 Answers16

65

For iOS 5.1.1 and lower, use the openURL method of UIApplication. It will perform the normal iPhone magical URL reinterpretation. so

[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]

should invoke the Google maps app.

From iOS 6, you'll be invoking Apple's own Maps app. For this, configure an MKMapItem object with the location you want to display, and then send it the openInMapsWithLaunchOptions message. To start at the current location, try:

[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];

You'll need to be linked against MapKit for this (and it will prompt for location access, I believe).

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • 2
    Wiki with description of params: http://mapki.com/wiki/Google_Map_Parameters Apple iphone os programming guide Appendix A has a list of supported parameters on the iphone. – Carl Coryell-Martin Aug 12 '09 at 05:31
  • This is leaky - instead of `[[NSURL alloc] initWithString:]` you should use `[NSURL URLWithString:]` as it provides an autoreleased object. – Tim Feb 06 '12 at 21:37
  • @Tim, Fixed - thanks. In fairness to me, it didn't matter when this answer was first posted, as the app would terminate as soon as the call completed. – Adam Wright Feb 06 '12 at 22:04
  • It always matters! Also, instead of `someUIApplication` you could just put `[UIApplication sharedApplication]` (since I don't know actually know any other situation where you'd get `UIApplication` besides making one) – Tim Feb 07 '12 at 22:05
  • It really doesn't always matter. I won't deny it's a sensible rule, but there was no operational difference between freeing it yourself and letting the runtime/OS do it in this case. – Adam Wright Feb 07 '12 at 22:20
  • To open Maps with a custom location (lat, lon) see this answer: http://stackoverflow.com/questions/12692924/mkmapitem-with-specific-location/12793469#12793469 – Eneko Alonso Oct 09 '12 at 05:42
  • 2
    According to Apple's documentation (http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html) you should use maps.apple.com as in: http://maps.apple.com/?q=London This would avoid the need to put in different code depending on the OS version since in iOS 5, it will redirect to Google Maps. – Matt James Dec 23 '12 at 16:36
  • @MattJames this is the most compatible and "right" way to do it! You should post as a separate answer. – dooleyo Oct 31 '13 at 16:00
32

Exactly. The code that you need to achieve this is something like that:

UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];

since as per the documentation, UIApplication is only available in the Application Delegate unless you call sharedApplication.

MonsieurDart
  • 6,005
  • 2
  • 43
  • 45
davidmytton
  • 38,604
  • 37
  • 87
  • 93
  • 7
    You're leaking the NSURL. I would recommend: [app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?g=London"]] so that it is autoreleased. – Matthew McGoogan Mar 11 '10 at 16:13
29

To open Google Maps at specific co-ordinates, try this code:

NSString *latlong = @"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

You can replace the latlong string with the current location from CoreLocation.

You can also specify the zoom level, using the (”z“) flag. Values are 1-19. Here's an example:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];

prakash
  • 58,901
  • 25
  • 93
  • 115
Jane Sales
  • 13,526
  • 3
  • 52
  • 57
  • 2
    Somehow it doesn't work correctly for me with "ll", I have to use "q" instead. For example http://maps.google.com/maps?q=48.8588054,2.3030451 and http://maps.google.com/maps?ll=48.8588054,2.3030451 do not point to the same address. – Enzo Tran Oct 11 '11 at 19:19
18

There is also now the App Store Google Maps app, documented at https://developers.google.com/maps/documentation/ios/urlscheme

So you'd first check that it's installed:

[[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]];

And then you can conditionally replace http://maps.google.com/maps?q= with comgooglemaps://?q=.

Michael Baltaks
  • 2,141
  • 19
  • 22
13

Here's the Apple URL Scheme Reference for Map Links: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

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.

**See the link above for the list of supported parameters.

Community
  • 1
  • 1
Harry Love
  • 1,920
  • 1
  • 25
  • 25
10

If you are using ios 10 then please don't forget to add Query Schemes in Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>comgooglemaps</string>
</array>

If you are using objective-c

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]]) {
    NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    } else { 
    NSString *string = [NSString stringWithFormat:@"http://maps.google.com/maps?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    }

If you are using swift 2.2

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: string)!)
}

If you are using swift 3.0

if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: string)!)
}
Ruchin Somal
  • 1,073
  • 12
  • 14
2

Just call this method and add Google Maps URL Scheme into your .plist file same as this Answer.

Swift-4 :-

func openMapApp(latitude:String, longitude:String, address:String) {

    var myAddress:String = address

    //For Apple Maps
    let testURL2 = URL.init(string: "http://maps.apple.com/")

    //For Google Maps
    let testURL = URL.init(string: "comgooglemaps-x-callback://")

    //For Google Maps
    if UIApplication.shared.canOpenURL(testURL!) {
        var direction:String = ""
        myAddress = myAddress.replacingOccurrences(of: " ", with: "+")

        direction = String(format: "comgooglemaps-x-callback://?daddr=%@,%@&x-success=sourceapp://?resume=true&x-source=AirApp", latitude, longitude)

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
    //For Apple Maps
    else if UIApplication.shared.canOpenURL(testURL2!) {
        var direction:String = ""
        myAddress = myAddress.replacingOccurrences(of: " ", with: "+")

        var CurrentLocationLatitude:String = ""
        var CurrentLocationLongitude:String = ""

        if let latitude = USERDEFAULT.value(forKey: "CurrentLocationLatitude") as? Double {
            CurrentLocationLatitude = "\(latitude)"
            //print(myLatitude)
        }

        if let longitude = USERDEFAULT.value(forKey: "CurrentLocationLongitude") as? Double {
            CurrentLocationLongitude = "\(longitude)"
            //print(myLongitude)
        }

        direction = String(format: "http://maps.apple.com/?saddr=%@,%@&daddr=%@,%@", CurrentLocationLatitude, CurrentLocationLongitude, latitude, longitude)

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }

    }
    //For SAFARI Browser
    else {
        var direction:String = ""
        direction = String(format: "http://maps.google.com/maps?q=%@,%@", latitude, longitude)
        direction = direction.replacingOccurrences(of: " ", with: "+")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
}

Hope, this is what you're looking for. Any concern get back to me. :)

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
  • It isn't worked. I tell the scenario. On button click i pass url with saddr and daddr and call _UIApplication.shared.canOpenURL(unwrappedURL)_. Any help – iSrinivasan27 Jan 24 '18 at 07:06
  • In Simulator, you don't have google maps Application. So your else condition will be execute. And URL opens in SAFARI Browser. Please uninstall the google maps application on device, check it. It will be works same as simulator. – Meet Doshi Jan 24 '18 at 07:13
  • Thanks, one ques off the topic. How can i check Gmap installed or not in iphone – iSrinivasan27 Jan 24 '18 at 07:17
  • Looks like you have not used iPhone yet. Just open Appstore, Search Google Maps. And check there is install option or open application option. And another option is, search google maps app directly in your open by pressing down gesture in your iPhone Home screen. – Meet Doshi Jan 24 '18 at 07:23
  • Yes, I'm not but better android user. i think i didn't ask question properly. how can i check google map app is installed in iPhone via code? to open it or redirect to app store use – iSrinivasan27 Jan 24 '18 at 07:26
  • Just check my code again. IF condition is for checking installed google maps or not via code. let me write that code again.. //For Google Maps let testURL = URL.init(string: "comgooglemaps-x-callback://") //For Google Maps if UIApplication.shared.canOpenURL(testURL!) { // installed } else { //not installed } – Meet Doshi Jan 24 '18 at 07:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163779/discussion-between-meet-doshi-and-07j91). – Meet Doshi Jan 24 '18 at 09:17
  • @MeetDoshi what does `%@` symbols stand for? Can you point me to a resource where I can learn more about it. Many thanks – bibscy Apr 11 '18 at 12:44
2

For the phone question, are you testing on the simulator? This only works on the device itself.

Also, openURL returns a bool, which you can use to check if the device you're running on supports the functionality. For example, you can't make calls on an iPod Touch :-)

Jane Sales
  • 13,526
  • 3
  • 52
  • 57
1

"g" change to "q"

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]
Pavel Kurta
  • 113
  • 2
  • 6
1

If you're still having trouble, this video shows how to get "My maps" from google to show up on the iphone -- you can then take the link and send it to anybody and it works.

http://www.youtube.com/watch?v=Xo5tPjsFBX4

Tex
  • 11
  • 1
1

For move to Google map use this api and send destination latitude and longitude

NSString* addr = nil;
     addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", destinationLat,destinationLong];

NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
DURGESH
  • 2,373
  • 1
  • 15
  • 13
0

iPhone4 iOS 6.0.1 (10A523)

For both Safari & Chrome. Both latest version up till now (2013-Jun-10th).

The URL scheme below also work. But in case of Chrome only works inside the page doesn't work from the address bar.

maps:q=GivenTitle@latitude,longtitude

3shmaoy
  • 11
  • 2
0

If you need more flexibility than the Google URL format gives you or you would like to embed a map in your application instead of launching the map app here is an example.

It will even supply you with the source code to do all of the embedding.

Simon
  • 31,675
  • 9
  • 80
  • 92
Lee
  • 751
  • 5
  • 2
0
**Getting Directions between 2 locations**

        NSString *googleMapUrlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=%@,%@", @"30.7046", @"76.7179", @"30.4414", @"76.1617"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapUrlString]];
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
0

Working Code as on Swift 4:

Step 1 - Add Following to info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>

Step 2 - Use Following Code to Show Google Maps

    let destinationLatitude = "40.7128"
    let destinationLongitude = "74.0060"

    if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
        if let url = URL(string: "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }else{
        if let url = URL(string: "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23
0

If you need more flexabilty than the Google URL format gives you or you would like to embed a map in your application instead of launching the map app there is an example found at https://sourceforge.net/projects/quickconnect.

RAVI
  • 151
  • 2
  • 12