7

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.

Nate
  • 31,017
  • 13
  • 83
  • 207
EmptyStack
  • 51,274
  • 23
  • 147
  • 178

4 Answers4

11

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.

Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • Thanks Man.. When I pass the Current%20Location as the saddr. The Maps app is not even opening. **[NSString stringWithFormat: @<"http://maps.google.com/maps?saddr=Current%20Location&daddr=%f,%f">, destCoordinate.latitude, destCoordinate.longitude]** is the code I am using. – EmptyStack Jan 04 '11 at 06:09
  • Put the code in your question or put backticks around it in your comment -- it's not readable as-is. I'm positive it works, so it sounds like a syntax issue. – Matthew Frederick Jan 04 '11 at 06:34
  • Sorry.. The code I am using is `[NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current%20Location&daddr=%f,%f", destCoordinate.latitude, destCoordinate.longitude]` – EmptyStack Jan 04 '11 at 06:39
  • You might have to escape the `%` character, I don't recall from when I last did it. Use `Current\%20Location`. If not, then NSLog your url string after the formatting to ensure the rest looks right. – Matthew Frederick Jan 04 '11 at 06:40
  • This is the log value of url string `http://maps.google.com/maps?saddr=Current 24000000000cation&daddr=2.000000,0.000000`. It inserts some spaces between *Current* and *24000000000cation* – EmptyStack Jan 04 '11 at 06:49
  • Yeah, see where it says `Current 24000000000cation`? Obviously that's not right. Put the backslash before the `%` like I suggested above and it will solve the problem. – Matthew Frederick Jan 04 '11 at 06:51
  • YES you are correct.. I had to escape the %. One more thing, I saw in few posts saying its not possible to just pass something like "Current Location" to represent the Current Location, and we have to find it using the CLLocationManager. But you have given me a great solution. It works. Thank you so much... – EmptyStack Jan 04 '11 at 07:11
  • Great, glad it worked for you. I updated the answer to include the escape character so future readers won't have to read the comments, too. – Matthew Frederick Jan 04 '11 at 07:21
  • 2
    Can anyone get this to work on iPad? On iPhone/iPod touch it's fine, but on iPad it seems to start at "Current, MO" instead. :-o – Joe D'Andrea Mar 13 '11 at 15:12
  • Hi simon i am trying to application similar like yours to get the users current location but the line 1 of your code give me an that invalid initializer.I have tried many ways to resolve it. What may be the problem.Please help – Rocky Mar 16 '11 at 06:45
  • Comment on my own dumbness: of course you have to escape the % character in an `NSString stringWithFormat` -- clearly I wasn't paying attention. – Matthew Frederick Apr 18 '11 at 22:40
  • 2
    @MatthewF and others - I'm confused by these comments, since the correct way to escape a percentage sign is %% unless I'm mistaken. In fact, I've tried \% and it didn't resolve into a percentage sign at all, it produced a random series of digits. So, for future readers may I suggest: http://maps.google.com/maps?saddr=Current%%20Location&daddr..... – Carlos P Aug 27 '11 at 17:48
  • @Carlos Right you are! Can't believe I missed that. Stranger still, I thought I'd tested the code, but I must not have. I'll fix the answer. – Matthew Frederick Aug 29 '11 at 06:33
7

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.

pazustep
  • 441
  • 4
  • 9
  • Is it? I haven't tested it with other languages. Thanks for your hint. I will take this into consideration. – EmptyStack Feb 09 '11 at 03:49
5

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.

martip
  • 106
  • 2
  • 4
  • Calling currentLocale on a device with the language set to English and the Currency and Date settings set to German will return "de", but Google Maps will still expect "Current Location". See [this question](http://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios) on how to get the current language setting. – THM Jun 18 '12 at 09:25
2

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

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • MKMapItem makes it really simple to show in the Maps App directions from your current location to a POI. But in order to show those POIs that are nearby, you'll still have to use Core Location to get your current location for the lat/long in your sql. The mapItemForCurrentLocation can't be used as it doesn't have a placemark and so won't yield the lat/long you need. – SPA Sep 24 '12 at 08:13
  • @SPA, huh? What SQL are you talking about? Did you mean to say "URL"? There's still no URL necessary with this iOS 6 solution. This question is about finding directions from the current location, to a destination. There is no need to use `mapItemForCurrentLocation`. – Nate Sep 24 '12 at 08:39
  • No, I mean SQL. If you have a database of places, to present the nearest ones you need the current location as lat/long to make the SQL Select statement. So Core Location is still required. – SPA Sep 24 '12 at 09:53
  • @SPA, what question are you answering? This question says nothing about a database of places, in SQL or any other format. – Nate Sep 24 '12 at 09:58
  • It does say to a destination coordinate though. So that has to come from somewhere. The user might select it from a list. That list can only be populated if you know what the current location is. – SPA Sep 24 '12 at 10:45
  • @SPA, you're commenting on a completely different problem, that the question is not asking about. The poster already has a method to get the current location. The question is about how to route from the (most up-to-date) current location to a destination. My answer, and the others, tell how to do this. Your comments are irrelevant to this problem. I never said anything about extracting coordinates from `mapItemForCurrentLocation`. – Nate Sep 24 '12 at 20:46