如何从我自己的原生应用程序中启动Google Maps iPhone应用程序?

时间:2008-08-27 13:15:40

标签: ios objective-c google-maps

Apple Developer Documentation(链接现已死亡)说明如果您在网页上放置一个链接,然后在iPhone上使用Mobile Safari时点击该链接,那么作为iPhone标准提供的Google地图应用程序将推出。

如何在我自己的原生iPhone应用程序中启动具有特定地址的相同Google地图应用程序(即通过Mobile Safari不是网页),就像点击通讯录中的地址启动地图一样?

注意:这仅适用于设备本身。不在模拟器中。

16 个答案:

答案 0 :(得分:65)

对于iOS 5.1.1及更低版本,请使用openURL的{​​{1}}方法。它将执行正常的iPhone魔法URL重新解释。所以

UIApplication

应该调用Google地图应用。

从iOS 6开始,您将调用Apple自己的地图应用。为此,请使用要显示的位置配置[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]] 对象,然后向其发送MKMapItem消息。要从当前位置开始,请尝试:

openInMapsWithLaunchOptions

您需要将此链接与MapKit相关联(我相信它会提示进行位置访问)。

答案 1 :(得分:31)

完全。您需要实现此目的的代码是这样的:

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

as per the documentation起,除非您调用sharedApplication,否则UIApplication仅在Application Delegate中可用。

答案 2 :(得分:29)

要在特定坐标下打开Goog​​le地图,请尝试以下代码:

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

您可以使用CoreLocation中的当前位置替换latlong字符串。

您还可以使用(“z”)标志指定缩放级别。值为1-19。这是一个例子:

  

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

答案 3 :(得分:18)

现在还有App Store Google Maps应用程序,记录在https://developers.google.com/maps/documentation/ios/urlscheme

所以你首先要检查它是否已安装:

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

然后,您可以有条件地将http://maps.google.com/maps?q=替换为comgooglemaps://?q=

答案 4 :(得分:13)

以下是地图链接的Apple URL方案参考:https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

  

创建有效地图链接的规则如下:

     
      
  • 域必须是google.com,子域必须是maps或ditu。
  •   
  • 如果查询包含site作为键,local作为值,则路径必须是/,/ maps,/ local或/ m。
  •   
  • 路径不能是/ maps /*.
  •   
  • 必须支持所有参数。有关支持的参数列表,请参阅表1. **。
  •   
  • 如果值是网址,则参数不能为q = *(因此未选取KML)。
  •   
  • 参数不能包含view = text或dirflg = r。
  •   

**请参阅上面的链接以获取支持的参数列表。

答案 5 :(得分:9)

如果您使用的是ios 10,请不要忘记在Info.plist中添加查询方案

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

如果您使用的是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]];
    }

如果您使用的是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)!)
}

如果您使用的是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)!)
}

答案 6 :(得分:2)

对于手机问题,你在模拟器上进行测试吗?这仅适用于设备本身。

此外,openURL返回一个bool,您可以使用它来检查您运行的设备是否支持该功能。例如,您无法在iPod Touch上拨打电话: - )

答案 7 :(得分:2)

只需调用此方法,然后将Google Maps URL Scheme添加到与此Answer相同的.plist文件中。

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

希望,这是你正在寻找的。任何关注都会回复给我。 :)

答案 8 :(得分:1)

“g”更改为“q”

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

答案 9 :(得分:1)

如果您仍然遇到问题,此视频会显示如何从Google上获取“我的地图”以显示在iPhone上 - 然后您可以将链接发送给任何人并且它可以正常运行。

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

答案 10 :(得分:1)

要移至Google地图,请使用此API并发送目的地纬度和经度

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

答案 11 :(得分:0)

如果您需要比Google网址格式更多的灵活性,您或者想要在应用中嵌入地图,而不是启动地图应用here is an example

它甚至会为您提供执行所有嵌入的源代码。

答案 12 :(得分:0)

如果您需要比Google网址格式更多的灵活性,您或者想要在应用中嵌入地图而不是启动地图应用,可以在https://sourceforge.net/projects/quickconnect找到一个示例。

答案 13 :(得分:0)

iPhone4 iOS 6.0.1(10A523)

对于Safari和&amp;铬。 最新版本至今(2013年6月至10月)。

下面的URL方案也有效。 但是,如果Chrome仅在页面内部工作,则无法从地址栏中运行。

地图:Q = GivenTitle @纬度,经度

答案 14 :(得分: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]];

答案 15 :(得分:0)

与Swift 4一样的工作代码:

步骤1-将以下信息添加到info.plist

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

第2步-使用以下代码显示Google地图

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