Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Maps app from Code - Where/How to find the "Current Location"?

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:

  1. Is this good practice to calculate the Current Location, at the time the Maps app is called?
  2. Will [self getCurrentLocation] return the Current Location before openURL gets called?
  3. 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.

like image 953
EmptyStack Avatar asked Jan 04 '11 05:01

EmptyStack


People also ask

How do I get my current location on Google Maps API?

To get current location using HTML5 Geolocation with Google Maps, you need to set an API key for Google Static Maps API. Go to https://console.developers.google.com and get a free API key for Google Map. Add this key to the code to work Geolocation with it.


4 Answers

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%%20Location and Maps will determine the user's current location itself.

%20 is 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.

like image 185
Matthew Frederick Avatar answered Oct 19 '22 09:10

Matthew Frederick


Using "Current Location" as saddr only works if the user has the system language set to English. The best options is really to get the current position from Core Location and use that as saddr.

like image 7
pazustep Avatar answered Oct 19 '22 09:10

pazustep


As pazustep pointed out, "Current Location" works only for English. In Italian, for example, the correct string is "Posizione attuale".

Sniffing in the iPhone firmware I detected all the "Current Location" translations and I wrote a class that provides the correct string needed for any (currently) supported language.

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

like image 5
martip Avatar answered Oct 19 '22 08:10

martip


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

Basically, you will 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];

In order to support both iOS 6+ and pre iOS 6 in the same code, I'd recommend using something like this code that Apple has on the MKMapItem API doc page:

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

This would assume that your Xcode Base SDK is iOS 6 (or Latest iOS).

In this other answer, I offer a robust technique for iOS 5.1 and lower

like image 2
Nate Avatar answered Oct 19 '22 07:10

Nate