13

Before IOS 6, I was using this URL scheme to open the native maps app and find directions from the users current location to an address that I created.

http://maps.google.com/maps?daddr=" + address + "&saddr=Current+Location

This was working great, but now that they got rid google maps with IOS 6, we had to check which IOS version they were on and then refer them to the new apple maps url scheme if they were using IOS 6.0 or greater. The new url scheme we are using is this....

http://maps.apple.com/maps?daddr=" + address + "&saddr=Current+Location

This is based on the new documentation for map url schemes, which can be found here..

Anyways, I've tested it a bunch and it boils down to the new apple maps does recognize Current Location, like google maps did.

Does anyone know how I fix this?

Keep in mind I am building a html app with phone gap, so using native code to set the starting address to current location won't help me.

Ben Doherty
  • 151
  • 1
  • 1
  • 9
  • You forgot to place the url to the documentation. – Dion Segijn Oct 12 '12 at 09:43
  • Woops sorry for getting back to you so late here is the link the documentation although it won't help much. http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html#//apple_ref/doc/uid/TP40007894-SW1 – Ben Doherty Oct 15 '12 at 20:37
  • Use %20 instead of + (plus) symbol. The plus symbol is not the correct way to add a space in an url. Some search engines and form submits however uses plus as a substitude for space, but the correct way is %20. I have verified on an iPad that the above URL does open a navigation when the text is &saddr=Current%20Location – nivs1978 Jun 22 '15 at 12:23

4 Answers4

10

I am having the same problem. I haven't found a solution yet but if you leave off the saddr

http://maps.apple.com/maps?daddr=" + address

it will just ask them where to start and the first option is "Current Location" so when they click "Current Location" it will show the map correctly.

If anyone finds a better solution please post it as I am still looking for a better solution.

Tony Brix
  • 4,085
  • 7
  • 41
  • 53
3

You can use my method:

    <script type="text/javascript">

        var link = "maps:saddr=YPlat,YPlong&daddr=42.118599,-72.625122";
        navigator.geolocation.getCurrentPosition(showPosition);

        function showPosition(position)
        {

            link = link.replace("YPlat",position.coords.latitude);
            link = link.replace("YPlong",position.coords.longitude);

            window.location = link;
        } 
    </script>

confirmed with iOS 5.1 and iOS 6

dod_basim
  • 300
  • 1
  • 12
  • This would have to be in some form of UIWebView or something though? I do see how this could be used not from within a UIWebView but it'd still require requesting permission to use the GPS. – Brad Moore Dec 16 '12 at 07:00
3

Just pass "Current Location" as the source address:

http://maps.apple.com/maps?saddr=Current%20Location&daddr=Your_Address
Spaceship09
  • 341
  • 3
  • 8
2

You can get the coordinates of the current location using CLLocationManager, or its wrapper DKLocationManager (on github), created by Keith Pitt.

Once you have the coordinates, you can use the following code sample.

+ (void) openDirectionFrom:CLLocation* currentLocation To:(NSString*) daddr {
    NSString* urlStr;
    NSString* saddr = @"Current+Location";

    if ([[UIDevice currentDevice] systemVersion] floatValue] >=6) {
        //iOS 6+, Should use map.apple.com. Current Location doesn't work in iOS 6 . Must provide the coordinate.
        if ((currentLocation.coordinate.latitude != kCLLocationCoordinate2DInvalid.latitude) && (currentLocation.coordinate.longitude != kCLLocationCoordinate2DInvalid.longitude)) {
            //Valid location.
            saddr = [NSString stringWithFormat:@"%f,%f", currentLocation.coordinate.latitude,currentLocation.coordinate.longitude];
            urlStr = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%@&daddr=%@", saddr, daddr];
        } else {
            //Invalid location. Location Service disabled.  
            urlStr = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%@", daddr];
        }
    } else {
        // < iOS 6. Use maps.google.com
        urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@", saddr, daddr];
    }

    [(UIApplicationWithEvents*)[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];

}
Xiaochen Du
  • 176
  • 10