苹果开发者文档 (链接现已失效)解释说,如果您在网页中放置一个链接,然后在 iPhone 上使用 Mobile Safari 时单击该链接,则 iPhone 标配的 Google 地图应用程序将会启动。

如何从我自己的本机 iPhone 应用程序(即,不是通过 Mobile Safari 访问的网页)与点击通讯录中的地址启动地图的方式相同吗?

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

有帮助吗?

解决方案

对于 iOS 5.1.1 及更低版本,请使用 openURL 的方法 UIApplication. 。它将执行正常的 iPhone 神奇 URL 重新解释。所以

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

应该调用 Google 地图应用程序。

从 iOS 6 开始,您将调用 Apple 自己的地图应用程序。为此,配置一个 MKMapItem 对象与您要显示的位置,然后将其发送 openInMapsWithLaunchOptions 信息。要从当前位置开始,请尝试:

[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];

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

其他提示

确切地。实现此目的所需的代码类似于:

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

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

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

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

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

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

[[UIApplication共享应用程序] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];

现在还有 App Store Google 地图应用程序,记录于 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

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

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

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

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

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

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

只需调用此方法并将 Google 地图 URL 方案添加到您的 .plist 文件中,与此相同 回答.

斯威夫特-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!)
        }
    }
}

希望,这就是您正在寻找的。有任何疑虑请回复我。:)

将“g”更改为“q”

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

如果您仍然遇到问题,该视频演示了如何从 google 获取“我的地图”以显示在 iPhone 上 - 然后您可以获取该链接并将其发送给任何人,这样就可以了。

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

要移动到 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];

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

如果您需要比 Google URL 格式提供的更多灵活性,或者您希望在应用程序中嵌入地图而不是启动地图应用程序 这是一个例子.

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

iPhone4 iOS 6.0.1 (10A523)

适用于 Safari 和 Chrome。到目前为止(2013年6月10日)都是最新版本。

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

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

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

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)
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top