I am opening Maps app to show directions from user’s Current Location to a destination coordinate, from my code. I am using the following code to open the Maps app. I am calling this code when a button is pressed. getCurrentLocation is a method that returns the recently updated location.
- (void)showDirectionsToHere {
CLLocationCoordinate2D currentLocation = [self getCurrentLocation]; // LINE 1
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
currentLocation.latitude,
currentLocation.longitude,
destCoordinate.latitude,
destCoordinate.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
Here [self getCurrentLocation] in LINE 1 uses CLLocationManager to determine the Current Location and returns the value.
Note: I have not yet implemented the code in LINE1. I’ve just planned to do it that way.
My questions are:
- Is this good practice to calculate the Current Location, at the time the Maps app is called?
- Will
[self getCurrentLocation] return the Current Location beforeopenURLgets called? - Do I have to determine the Current Location well before opening the Maps app?
I am little bit confused about these things. Kindly guide me. Thanks.
You don’t have to determine the user’s current location yourself, the Maps app will take care of it.
Instead of passing a latitude/longitude pair you can pass
Current%%20Locationand Maps will determine the user’s current location itself.%20is a url-encoded space character, and the extra%escapes the actual%so it won’t be interpreted as a format substitution.Thanks to @Carlos P for pointing out my escape character blunder in the original answer.