Objective-c – iOS Google Maps Question

iphoneobjective c

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?

Best Answer

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]];