I have reviewed the Apple Documentation (http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html) on sending a user to the Google Maps App and I have a question, "How do I have it automatically bring up directions from the user's current location to a predetermined lat/long location?
The Apple Docs say to use "saddr=" and "daddr=" for the source and destination respectively, but it does开发者_运维技巧n't say how to acquire the user's current location. Is there some sort of keyword such as "saddr=Current" or "saddr=Here" that will get the user's location?
Obviously, this doesn't work:
[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=Here&daddr=Chicago"]];
..if I was trying to send the user to Chicago. Any thoughts?
SOURCE: How to invoke iPhone Maps for Directions with Current Location as Start Address
You need to use Core Location to get the current location, but with that lat/long pair, you can get Maps to route you from there, to a street address. Like so:
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]];
I don't think you can do this directly. But you can put coordinates into the parameters by using the format saddr=lat,long
(e.g. addr=53.453209,-0.32930
). So if you work out the user's current location in your app before you despatch to Google Maps, you get an approximation of the functionality.
You can just use Current+Location in the saddr part of the url. That works on iOS, but I can't find what works for Android.
Simply calling the Google Maps URL with just the daddr
parameter set, makes Google Maps insert the user's current location automatically in the From field.
http://maps.google.de/maps?daddr=SOMEADDRESS
I believe that saddr=Current%20Location&daddr=...
is what you want.
You can always use the reverse geocoder in iOS to get the current locations address (given that the user lets you acquire their location) and use it in the URL, here MKReverseGeocoder is a reference to the class used for reverse geocoding.
-Daniel
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]];
}
You can use CurrentLocation Coordinate in the origin part and destinationLocation Coordinate in the destination part in the url. That works on iOS...
https://maps.googleapis.com/maps/api/directions/json?origin=(originCoordinate)&destination=(destinationCoordinate)&mode=driving&key=YOUR-KEY
精彩评论