How can I launch the Google Maps iPhone application from within my own native application?

IosObjective CGoogle Maps

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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).

Solution 2 - Ios

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.

Solution 3 - Ios

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"]];

Solution 4 - Ios

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=.

Solution 5 - Ios

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.

Solution 6 - Ios

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)!)
}

Solution 7 - Ios

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 :-)

Solution 8 - Ios

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. :)

Solution 9 - Ios

"g" change to "q"

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

Solution 10 - Ios

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

Solution 11 - Ios

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];

Solution 12 - Ios

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.

Solution 13 - Ios

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.

Solution 14 - Ios

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

Solution 15 - Ios

**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]];

Solution 16 - Ios

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)
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondavidmyttonView Question on Stackoverflow
Solution 1 - IosAdam WrightView Answer on Stackoverflow
Solution 2 - IosdavidmyttonView Answer on Stackoverflow
Solution 3 - IosJane SalesView Answer on Stackoverflow
Solution 4 - IosMichael BaltaksView Answer on Stackoverflow
Solution 5 - IosHarry LoveView Answer on Stackoverflow
Solution 6 - IosRuchin SomalView Answer on Stackoverflow
Solution 7 - IosJane SalesView Answer on Stackoverflow
Solution 8 - IosMeet DoshiView Answer on Stackoverflow
Solution 9 - IosPavel KurtaView Answer on Stackoverflow
Solution 10 - IosTexView Answer on Stackoverflow
Solution 11 - IosDURGESHView Answer on Stackoverflow
Solution 12 - IosLeeView Answer on Stackoverflow
Solution 13 - IosRAVIView Answer on Stackoverflow
Solution 14 - Ios3shmaoyView Answer on Stackoverflow
Solution 15 - IosMannam BrahmamView Answer on Stackoverflow
Solution 16 - IosAbhirajsinh ThakoreView Answer on Stackoverflow