0

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 doesn'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?

startuprob
  • 1,917
  • 5
  • 30
  • 45

8 Answers8

3

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]];
Community
  • 1
  • 1
Josh
  • 12,448
  • 10
  • 74
  • 118
  • I am getting an "Invalid Initializer" error on the CLLocation line. – startuprob Dec 13 '10 at 20:26
  • It looks like you need to add a -(CLLocationCoordinate2D)getCurrentLocation; method to the class you're calling it this chunk of code in, that returns a CLLocationCoordinate2D, for use in the url instantiation. – Josh Dec 13 '10 at 20:29
1

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

Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
1

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.

grahamparks
  • 16,130
  • 5
  • 49
  • 43
1

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.

Stephen
  • 11
  • 1
0

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

Daniel
  • 22,363
  • 9
  • 64
  • 71
0

I believe that saddr=Current%20Location&daddr=... is what you want.

Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • "Current Location" won't work because it breaks up the URL."Current", "CurrentLocation", and "Current_Location" all don't work either. – startuprob Dec 13 '10 at 20:20
  • Didn't notice that you weren't escaping your string. I'll edit it now with the space character escaped. – Matthew Frederick Dec 13 '10 at 20:28
  • 3
    Unfortunately, &saddr=Current%20Location doesn't work if the phone is set to a non English language. – chris Feb 02 '11 at 18:26
  • @chris Ah, good point, I'd not tested it with another language. I imagine that there are local language versions that work, though I've not been able to find anything. – Matthew Frederick Feb 03 '11 at 07:39
  • 1
    iPad2, iOS 4.3: using this setup, the Maps app matches curent location to something else (in my case "Current, Marilao, Philippines" which is no where near where I am). – William Denniss May 05 '11 at 11:11
  • @William Unfortunately it only works in markets where Google is expecting an English language query -- perhaps it's expecting Filipino or some other language where you are. You can localize the the query, using the local-language equivalent of "current location" that Google is expecting. Unfortunately I've not been able to find a list of precisely what Google expects in each language and locale, but you might try the most likely translations to see what you get. – Matthew Frederick May 05 '11 at 15:59
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]];
}
0

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