12

I have two variables - Destination and Source - and, using Phonegap, I'm trying to use jQuery to open the external iPhone Apple Maps app with directions.

The code I'm using looks like this:

var url = 'http://maps.apple.com/?q=daddr='+destination+'&saddr='+source;
window.location = url;

Whenever I click the relevant button however, it opens a new view in-app with Google directions as a webview, and I cannot return to the original app.

How can link be opened instead with the iOS default map application?

Ben
  • 8,894
  • 7
  • 44
  • 80
  • You might want to take a look at http://stackoverflow.com/questions/12504294/programmatically-open-maps-app-in-ios-6 – Iain Collins Jan 24 '13 at 15:01
  • I saw that but unfortunately I'm coding with Phonegap in HTML and Javascript so Objective-C isn't the language I'm using to compile - but I've found the solution I needed, thanks for your help! – Ben Jan 24 '13 at 15:28
  • @BenPearlKahan I have the same problem, can you show me your soluction? Thanks – kongkea Feb 15 '13 at 01:12

4 Answers4

25

Changing http://maps.apple.com/?q= to just maps: did the trick.


NB: make sure you write maps: and not map: as, while the latter will run the correct app, it will crash immediately after opening (thanks jprofitt)

Ben
  • 8,894
  • 7
  • 44
  • 80
  • Ah-hah! I was wondering if there was something just like that (a specific protocol handler) but hadn't found any documentation - didn't think to just try that! Thanks for posting that. – Iain Collins Jan 24 '13 at 15:30
  • Pleasure - was a relief to figure it out! – Ben Jan 24 '13 at 15:36
  • 1
    Interesting how this is in direct contradiction to Apple's documentation: http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html but this answer is what works! – David Boike Mar 26 '13 at 21:02
  • 2
    As a note for anyone who gets stuck on trying to figure this out: on iOS 7, if you use `map:` instead of `maps:`, it will in fact launch the Maps app, but will crash immediately. Check your spelling. (Ben, feel free to add this to your answer if you'd like) – jprofitt Nov 14 '13 at 23:34
  • @BenPearlKahan This works, but how can I pass latitude and longitude instead of a name? I was using http://maps.apple.com/maps?ll=51.83733,-8.3016 – a_rahmanshah Jan 30 '14 at 03:57
6

Today I was able to open the native Apple Maps app with marker from a Cordova app using BOTH of the follow URL schemes:

maps://maps.apple.com/?q={latitude},{longitude}

AND

maps://?q={latitude},{longitude}

In a link:

<a href="maps://maps.apple.com?q={latitude},{longitude}">
<!-- OR -->
<a href="maps://?q={latitude},{longitude}">

From JavaScript:

window.location.href = "maps://maps.apple.com/?q="+lat+","+lng;
// OR
window.location.href = "maps://?q="+lat+","+lng;

The app is running on an iOS device, iOS version 8.1.2, and cordova-ios is version 3.7.0.

Josh Buchea
  • 1,366
  • 14
  • 14
4

In my case, changing from http://maps.apple.com/?q= to only maps:?q= not solving the problem.

The working scheme that open native Apple Maps with marker is as follows:

maps://maps.apple.com/?q={latitude},{longitude}

full working code:

window.location.href = "maps://maps.apple.com/?q="+lat+","+lng;

Tested working with iOS7.1 simulator, might not works in later version. I don't have the opportunity to test. Sorry.

Supported Apple Maps parameters can be found here: https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

Credit goes to this SO link: Here

Community
  • 1
  • 1
Amir Fazwan
  • 463
  • 1
  • 4
  • 12
4

The code

I managed to get this working in 2018 on iOS 11.2 using the following format in iOS:

<a onclick="window.open('maps://?q=51.178847,-1.826160', '_system');">Open in Maps</a>

And this format on Android:

<a onclick="window.open('geo:0,0?q=51.178847,-1.826160', '_system');">Open in Maps</a>

Config permissions

Additionally, don't forget to add permissions to your config.xml file like so:

<allow-intent href="maps:*" />

In my project the geo: intention had existed from the beginning so it took my a while to figure out why this was working on Android and not iOS.

SamMakesCode
  • 164
  • 1
  • 4