关于ios:如何从自己的本机应用程序中启动Google Maps iPhone应用程序?

关于ios:如何从自己的本机应用程序中启动Google Maps iPhone应用程序?

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

Apple开发人员文档(现在链接已失效)说明,如果您在iPhone上使用Mobile Safari时将链接放在网页中然后单击它,则将启动iPhone随附提供的Google Maps应用程序。

如何在我自己的本机iPhone应用程序(即不是通过Mobile Safari的网页)中使用特定地址启动相同的Google Maps应用程序,就像在"通讯录"中轻按地址即可启动地图一样?

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


对于iOS 5.1.1及更低版本,请使用UIApplicationopenURL方法。它将执行普通的iPhone神奇URL重新解释。所以

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

应该调用Google地图应用。

从iOS 6开始,您将调用Apple自己的Maps应用。为此,使用要显示的位置配置MKMapItem对象,然后向其发送openInMapsWithLaunchOptions消息。要从当前位置开始,请尝试:

1
[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];

为此,您需要针对MapKit进行链接(我相信它会提示您进行位置访问)。


究竟。您需要实现的代码是这样的:

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

因为根据文档,UIApplication仅在Application Delegate中可用,除非您调用sharedApplication。


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

1
2
3
4
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"]];


现在也有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=


这是地图链接的Apple URL方案参考: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.

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


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

1
2
3
4
<key>LSApplicationQueriesSchemes</key>

 <string>comgooglemaps</string>
</array>

如果您使用的是Objective-C

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
8
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)!)
}

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

斯威夫特4:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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!)
        }
    }
}

希望这就是您要寻找的。如有任何疑问,请联系我。 :)


对于电话问题,您是否正在模拟器上进行测试?这仅适用于设备本身。

另外,openURL返回一个布尔值,您可以使用该布尔值来检查正在运行的设备是否支持该功能。例如,您不能在iPod Touch上拨打电话:-)


如果您仍然遇到问题,该视频将演示如何从Google获取"我的地图"以显示在iPhone上-然后您可以获取链接并将其发送给任何人,并且可以正常使用。


将" g"更改为" q"

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

要移动到Google地图,请使用此API并发送目标纬度和经度

1
2
3
4
5
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];

与Swift 4一样的工作代码:

步骤1-将以下内容添加到info.plist

1
2
3
4
5
<key>LSApplicationQueriesSchemes</key>

<string>googlechromes</string>
<string>comgooglemaps</string>
</array>

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

1
2
3
4
5
6
7
8
9
10
11
12
    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)
        }
    }

iPhone4 iOS 6.0.1(10A523)

适用于Safari和Chrome。
两种最新版本都到现在为止(2013年6月10日)。

下面的URL方案也可以使用。
但是如果是Chrome,则只能在页面内部使用,而不能通过地址栏访问。

地图:q = GivenTitle @ latitude,longtitude


1
2
3
4
**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]];

如果您需要的灵活性超出了Google URL格式所提供的灵活性,或者您想在应用程序中嵌入地图而不是启动地图应用程序,请参考以下示例。

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


如果您需要的灵活性超出了Google URL格式所提供的灵活性,或者您想在应用程序中嵌入地图而不是启动地图应用程序,请访问https://sourceforge.net/projects/quickconnect。


推荐阅读