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.
苹果开发者文档(link现在已经失效)解释说,如果你在网页上放置一个链接,然后在iPhone上使用移动Safari时点击它,谷歌地图应用程序将会在iPhone上作为标准提供。
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访问的web页面)中使用特定地址启动同一个谷歌地图应用程序,就像在联系人中点击一个地址启动地图一样?
NOTE: THIS ONLY WORKS ON THE DEVICE ITSELF. NOT IN THE SIMULATOR.
注意:这只适用于设备本身。不是在模拟器上。
15 个解决方案
#1
65
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和更低的版本,使用UIApplication的openURL方法。它将执行正常的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中,您将调用苹果自己的地图应用程序。为此,将一个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(我相信它将提示位置访问)。
#2
31
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.
因为根据文档,UIApplication只在应用程序委托中可用,除非您调用sharedApplication。
#3
28
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.
您可以用CoreLocation中的当前位置替换latlong字符串。
You can also specify the zoom level, using the (”z“) flag. Values are 1-19. Here's an example:
您还可以使用(“z”)标志指定缩放级别。值-。这里有一个例子:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@ " http://maps.google.com/maps?z=8 "]];
#4
18
There is also now the App Store Google Maps app, documented at https://developers.google.com/maps/documentation/ios/urlscheme
现在也有应用商店谷歌地图应用,记录在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 =。
#5
13
Here's the Apple URL Scheme Reference for Map Links: http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html
这里是苹果地图链接的URL模式引用:http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/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.
- 域名必须是google。com子域名必须是maps或ditu。
- The path must be /, /maps, /local, or /m if the query contains site as the key and local as the value.
- 路径必须是/、/maps、/local或/m(如果查询包含站点作为键,而本地作为值)。
- The path cannot be /maps/*.
- 路径不能是/maps/*。
- All parameters must be supported. See Table 1 for list of supported parameters**.
- 必须支持所有参数。支持参数列表见表1 *。
- A parameter cannot be q=* if the value is a URL (so KML is not picked up).
- 如果值是URL,则参数不能是q=*(因此不会选取KML)。
- The parameters cannot include view=text or dirflg=r.
- 参数不能包括view=text或dirflg=r。
**See the link above for the list of supported parameters.
** *支持参数列表见上面的链接。
#6
7
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)!)
}
#7
2
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打电话:-)
#8
1
"g" change to "q"
“g”更改为“q”
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]
#9
1
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.
如果你仍然有问题,这段视频展示了如何从谷歌获取“我的地图”,并在iphone上显示出来——然后你可以将链接发送给任何人,它就会工作。
http://www.youtube.com/watch?v=Xo5tPjsFBX4
http://www.youtube.com/watch?v=Xo5tPjsFBX4
#10
1
For move to Google map use this api and send destination latitude and longitude
要移动到谷歌地图,请使用此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
1
Just call this method and add Google Maps URL Scheme into your .plist file same as this Answer.
只需调用此方法并在.plist文件中添加谷歌映射URL方案即可。
Swift-4 :-
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. :)
希望,这就是你要找的。任何关心的事都可以告诉我。:)
#12
0
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.
如果您需要比谷歌URL格式更多的flexabilty,或者您想要在应用程序中嵌入映射,而不是启动地图应用程序,在https://sourceforge.net/projects/quickconnect中找到一个示例。
#13
0
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.
如果您需要比谷歌URL格式提供的更大的灵活性,或者您希望在应用程序中嵌入地图而不是启动地图应用程序,这里有一个例子。
It will even supply you with the source code to do all of the embedding.
它甚至会为您提供源代码来完成所有的嵌入。
#14
0
iPhone4 iOS 6.0.1 (10A523)
iPhone4 iOS 6.0.1中(10 a523)
For both Safari & Chrome. Both latest version up till now (2013-Jun-10th).
无论是Safari还是Chrome。到目前为止的最新版本(2013- 6 -10)。
The URL scheme below also work. But in case of Chrome only works inside the page doesn't work from the address bar.
下面的URL模式也可以。但如果Chrome只在页面内运行,地址栏就不能工作。
maps:q=GivenTitle@latitude,longtitude
地图:q = GivenTitle@latitude longtitude
#15
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]];
#1
65
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和更低的版本,使用UIApplication的openURL方法。它将执行正常的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中,您将调用苹果自己的地图应用程序。为此,将一个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(我相信它将提示位置访问)。
#2
31
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.
因为根据文档,UIApplication只在应用程序委托中可用,除非您调用sharedApplication。
#3
28
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.
您可以用CoreLocation中的当前位置替换latlong字符串。
You can also specify the zoom level, using the (”z“) flag. Values are 1-19. Here's an example:
您还可以使用(“z”)标志指定缩放级别。值-。这里有一个例子:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@ " http://maps.google.com/maps?z=8 "]];
#4
18
There is also now the App Store Google Maps app, documented at https://developers.google.com/maps/documentation/ios/urlscheme
现在也有应用商店谷歌地图应用,记录在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 =。
#5
13
Here's the Apple URL Scheme Reference for Map Links: http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html
这里是苹果地图链接的URL模式引用:http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/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.
- 域名必须是google。com子域名必须是maps或ditu。
- The path must be /, /maps, /local, or /m if the query contains site as the key and local as the value.
- 路径必须是/、/maps、/local或/m(如果查询包含站点作为键,而本地作为值)。
- The path cannot be /maps/*.
- 路径不能是/maps/*。
- All parameters must be supported. See Table 1 for list of supported parameters**.
- 必须支持所有参数。支持参数列表见表1 *。
- A parameter cannot be q=* if the value is a URL (so KML is not picked up).
- 如果值是URL,则参数不能是q=*(因此不会选取KML)。
- The parameters cannot include view=text or dirflg=r.
- 参数不能包括view=text或dirflg=r。
**See the link above for the list of supported parameters.
** *支持参数列表见上面的链接。
#6
7
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)!)
}
#7
2
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打电话:-)
#8
1
"g" change to "q"
“g”更改为“q”
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]
#9
1
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.
如果你仍然有问题,这段视频展示了如何从谷歌获取“我的地图”,并在iphone上显示出来——然后你可以将链接发送给任何人,它就会工作。
http://www.youtube.com/watch?v=Xo5tPjsFBX4
http://www.youtube.com/watch?v=Xo5tPjsFBX4
#10
1
For move to Google map use this api and send destination latitude and longitude
要移动到谷歌地图,请使用此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
1
Just call this method and add Google Maps URL Scheme into your .plist file same as this Answer.
只需调用此方法并在.plist文件中添加谷歌映射URL方案即可。
Swift-4 :-
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. :)
希望,这就是你要找的。任何关心的事都可以告诉我。:)
#12
0
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.
如果您需要比谷歌URL格式更多的flexabilty,或者您想要在应用程序中嵌入映射,而不是启动地图应用程序,在https://sourceforge.net/projects/quickconnect中找到一个示例。
#13
0
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.
如果您需要比谷歌URL格式提供的更大的灵活性,或者您希望在应用程序中嵌入地图而不是启动地图应用程序,这里有一个例子。
It will even supply you with the source code to do all of the embedding.
它甚至会为您提供源代码来完成所有的嵌入。
#14
0
iPhone4 iOS 6.0.1 (10A523)
iPhone4 iOS 6.0.1中(10 a523)
For both Safari & Chrome. Both latest version up till now (2013-Jun-10th).
无论是Safari还是Chrome。到目前为止的最新版本(2013- 6 -10)。
The URL scheme below also work. But in case of Chrome only works inside the page doesn't work from the address bar.
下面的URL模式也可以。但如果Chrome只在页面内运行,地址栏就不能工作。
maps:q=GivenTitle@latitude,longtitude
地图:q = GivenTitle@latitude longtitude
#15
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]];