iOS谷歌地图问题

时间:2010-12-13 19:51:04

标签: iphone objective-c

我在将用户发送到Google地图应用时查看了Apple文档(http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html),我有一个问题, “我如何让它自动将用户当前位置的路线调到预定的纬度/长度位置?

Apple Docs分别使用“saddr =”和“daddr =”作为源和目的地,但没有说明如何获取用户的当前位置。是否有某种关键字,例如“saddr = Current”或“saddr = Here”,这些关键字会获取用户的位置?

显然,这不起作用:

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

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

8 个答案:

答案 0 :(得分: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]];

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您可以在网址的saddr部分使用Current + Location。这适用于iOS,但我找不到适合Android的功能。

答案 3 :(得分:1)

只需使用daddr参数集调用Google地图网址,Google地图就会自动在发件人字段中插入用户的当前位置。

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

答案 4 :(得分:0)

您可以随时使用iOS中的反向地理编码器获取当前位置地址(假设用户允许您获取其位置)并在URL中使用它,此处MKReverseGeocoder是对用于的类的引用反向地理编码。

-Daniel

答案 5 :(得分:0)

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

答案 6 :(得分: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]];
}

答案 7 :(得分:0)

您可以在URL的原始部分中使用CurrentLocation坐标,并在目标部分中使用destinationLocation坐标。在iOS上可以使用...

https://maps.googleapis.com/maps/api/directions/json?origin=(originCoordinate)&destination=(destinationCoordinate)&mode=driving&key=YOUR-KEY