ios 如何从我自己的本机应用程序中启动 Google Maps iPhone 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I launch the Google Maps iPhone application from within my own native application?
提问by DavidM
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.
在苹果开发者文档(链接现在是死的)解释说,如果你把在网页中的链接,然后单击它,而在iPhone上,也作为标准配置提供与iPhone将推出谷歌地图应用程序中使用移动Safari浏览器。
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?
我如何才能从我自己的本机 iPhone 应用程序(即不是通过 Mobile Safari 的网页)中以特定地址启动相同的 Google 地图应用程序,就像点击通讯录中的地址启动地图一样?
NOTE: THIS ONLY WORKS ON THE DEVICE ITSELF. NOT IN THE SIMULATOR.
注意:这仅适用于设备本身。不在模拟器中。
采纳答案by Adam Wright
For iOS 5.1.1 and lower, use the openURL
method of UIApplication
. It will perform the normal iPhone magical URL reinterpretation. so
对于 iOS 5.1.1 及更低版本,使用 的openURL
方法UIApplication
。它将执行正常的 iPhone 神奇 URL 重新解释。所以
[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:
从 iOS 6 开始,您将调用 Apple 自己的地图应用程序。为此,MKMapItem
请使用要显示的位置配置对象,然后向其发送openInMapsWithLaunchOptions
消息。要从当前位置开始,请尝试:
[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];
You'll need to be linked against MapKit for this (and it will prompt for location access, I believe).
为此,您需要与 MapKit 进行链接(我相信它会提示进行位置访问)。
回答by DavidM
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.
因为根据文档,除非您调用 sharedApplication,否则 UIApplication 仅在应用程序委托中可用。
回答by Jane Sales
To open Google Maps at specific co-ordinates, try this code:
要在特定坐标处打开 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]];
You can replace the latlong string with the current location from CoreLocation.
您可以使用 CoreLocation 中的当前位置替换经纬度字符串。
You can also specify the zoom level, using the (”z“) flag. Values are 1-19. Here's an example:
您还可以使用 ("z") 标志指定缩放级别。值为 1-19。下面是一个例子:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@" http://maps.google.com/maps?z=8"]];
回答by Michael Baltaks
There is also now the App Store Google Maps app, documented at https://developers.google.com/maps/documentation/ios/urlscheme
现在还有 App Store Google Maps 应用程序,记录在https://developers.google.com/maps/documentation/ios/urlscheme
So you'd first check that it's installed:
因此,您首先要检查它是否已安装:
[[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]];
[[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]];
And then you can conditionally replace http://maps.google.com/maps?q=
with comgooglemaps://?q=
.
然后你可以有条件地替换http://maps.google.com/maps?q=
为comgooglemaps://?q=
.
回答by Harry Love
Here's the Apple URL Scheme Reference for Map Links: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
这是地图链接的 Apple URL Scheme 参考: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.
创建有效地图链接的规则如下:
- 域必须是 google.com,子域必须是 maps 或 ditu。
- 如果查询包含 site 作为键和 local 作为值,则路径必须是 /、/maps、/local 或 /m。
- 路径不能是 /maps/*。
- 必须支持所有参数。有关支持的参数列表,请参见表 1**。
- 如果值为 URL,则参数不能为 q=*(因此不会选取 KML)。
- 参数不能包含 view=text 或 dirflg=r。
**See the link above for the list of supported parameters.
**有关支持的参数列表,请参阅上面的链接。
回答by Ruchin Somal
If you are using ios 10 then please don't forget to add Query Schemes in Info.plist
如果您使用的是 ios 10,请不要忘记在 Info.plist 中添加查询方案
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
If you are using objective-c
如果您使用的是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
如果您使用的是 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
如果您使用的是 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)!)
}
回答by Meet Doshi
Just call this method and add Google Maps URL Scheme into your .plist file same as this Answer.
只需调用此方法并将 Google Maps URL Scheme 添加到与此答案相同的 .plist 文件中。
Swift-4 :-
斯威夫特-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. :)
希望,这就是你要找的。有任何问题都可以回复我。:)
回答by Jane Sales
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 :-)
此外,openURL 返回一个 bool,您可以使用它来检查您正在运行的设备是否支持该功能。例如,您无法在 iPod Touch 上拨打电话 :-)
回答by DURGESH
For move to Google map use this api and send destination latitude and longitude
要移动到 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];
回答by Pavel Kurta
"g" change to "q"
“g”改为“q”
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]