2010-12-13 182 views
0

我已经审阅了关于将用户发送到谷歌地图应用程序的Apple文档(http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html)和我有一个问题,“我该如何自动调出从用户当前位置到预先确定的纬度/经度位置的方向?”iOS谷歌地图问题

Apple Docs说对源代码使用“saddr =”和“daddr =”但它没有说明如何获取用户的当前位置是否有某种关键字,例如“saddr = Current”或“saddr = Here”可以获取用户的位置?

很明显,这个不起作用:

[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=Here&daddr=Chicago"]]; 

..如果我试图将用户发送到芝加哥。有什么想法吗?

回答

0

我相信saddr=Current%20Location&daddr=...是你想要的。

+0

“当前位置” 是行不通的,因为它打破了URL。 “当前”, “CurrentLocation” 和 “CURRENT_LOCATION” 一切都不能工作。 – startuprob 2010-12-13 20:20:05

+0

没有注意到你没有逃脱你的字符串。我现在将编辑它与空格字符转义。 – 2010-12-13 20:28:02

+3

不幸的是,如果手机设置为非英语语言,&saddr = Current%20Location不起作用。 – chris 2011-02-02 18:26:16

0

您可以随时使用反向地理编码器在iOS中获取的当前位置的地址(因为用户可以让你获得自己的位置),并在URL中使用它,这里MKReverseGeocoder是用于反向地理编码类的引用。

-Daniel

1

我不认为你可以直接做到这一点。但您可以使用格式saddr=lat,long(例如addr=53.453209,-0.32930)将坐标放入参数中。因此,如果您在发送到Google地图之前,先在应用中计算出用户的当前位置,则会获得近似的功能。

3

来源: How to invoke iPhone Maps for Directions with Current Location as Start Address

您需要使用的核心位置来获取当前位置,但与纬度/经度对,你可以映射到路线,你从那里到一个街道地址。像这样:

CLLocationCoordinate2D currentLocation = [self getCurrentLocation]; 
    NSString* address = @"123 Main St., New York, NY, 10001"; 
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@", 
        currentLocation.latitude, currentLocation.longitude, 
        [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 
+0

我在CLLocation行上收到“Invalid Initializer”错误。 – startuprob 2010-12-13 20:26:46

+0

它看起来像你需要添加 - (CLLocationCoordinate2D)getCurrentLocation;方法给你调用它的这个代码块,它返回一个CLLocationCoordinate2D,用于URL实例化。 – Josh 2010-12-13 20:29:19

1

您可以在url的saddr部分使用Current + Location。这适用于iOS,但我无法找到适用于Android的内容。

1

只需拨打daddr参数设置即可拨打Google Maps URL,这样Google地图就会自动在,字段中插入用户的当前位置。

http://maps.google.de/maps?daddr=SOMEADDRESS

+0

对不起,这只适用于模拟器(例如,当谷歌地图在Safari Mobile中启动而不是原生应用程序时) – 2012-09-14 18:04:53

0
NSString* directionsURL = [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude, mapPoint.coordinate.latitude, mapPoint.coordinate.longitude]; 
if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL] options:@{} completionHandler:^(BOOL success) {}]; 
} else { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL]]; 
}