Open the "Maps" application from the code - Where / How to find the "Current Location"?

I open the Maps application to show directions from the Current Location user to the destination coordinate from my code. To open the Maps application, I use the following code. I call this code when the button is clicked. getCurrentLocation is a method that returns a 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 the CLLocationManager to determine the current location and returns a value.

Note I have not implemented the code in LINE1 yet. I just planned to do it this way.

My questions:

  • Is it good practice to calculate your current location when calling up the Maps application?
  • Will [self getCurrentLocation ] return the current location before calling openURL ?
  • Do I have to determine my current location long before the Maps application opens?

I am a little confused by this. Please guide me. Thank.

+7
ios iphone google-maps core-location
Jan 04 2018-11-11T00:
source share
4 answers

You do not need to determine the current location of the user yourself, the Maps application will take care of this.

Instead of passing a latitude / longitude pair, you can go through Current%%20Location , and Maps themselves will determine the user's current location.

%20 is a space character in url encoding, and the extra % does the actual % , so it will not be interpreted as a format override.




Thanks to @Carlos P for pointing out my escape error in the original answer.

+11
Jan 04 2018-11-11T00:
source share

Using "Current Location" as saddr only works if the user has a system language installed in English. The best options are to really get the current position from Core Location and use it as saddr.

+7
Feb 08 2018-11-11T00:
source share

As Pazustpep noted, Current Location only works in English. In Italian, for example, the correct line is "Posizione attuale".

Smelling the iPhone firmware, I found all translations of "Current Location", and I wrote a class that contains the correct string needed for any (currently) supported language.

There is a post about this on this blog (source code): http://www.martip.net/blog/localized-current-location-string-for-iphone-apps .

+5
May 4 '11 at
source share

You can use the new MKMapItem class for iOS 6. See Apple API docs here.

Basically, you'll use something like this if routing to destination coordinates ( destCoordinate ):

  MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: destCoordinate addressDictionary: nil]; MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place]; destination.name = @"Name Here!"; NSArray* items = [[NSArray alloc] initWithObjects: destination, nil]; NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsDirectionsModeKey, nil]; [MKMapItem openMapsWithItems: items launchOptions: options]; 

To support both iOS 6+ and pre iOS 6 in the same code, I would recommend using something like this code that Apple has on the MKMapItem API document page:

 Class itemClass = [MKMapItem class]; if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // iOS 6 MKMapItem available } else { // use pre iOS 6 technique } 

Suppose your Xcode Base SDK is iOS 6 (or Latest iOS ).

In this other answer, I propose a reliable technique for iOS 5.1 and below.

+2
Sep 22
source share



All Articles