10

I am using RN 0.20 for iOS. I want to make a link where if you click that link, it will redirect you to the Map apps available in the phone with its coordinate. This is what I have done using Linking component.

redirectToMap() {
    Linking.canOpenURL('geo:37.484847,-122.148386').then(supported => {
        if (supported) {
            Linking.openURL('geo:37.484847,-122.148386');
        } else {
            console.log('Don\'t know how to go');
        }
    }).catch(err => console.error('An error occurred', err));
}

But it keeps giving me 'Don't know how to go'. However, I see from its documentation in https://facebook.github.io/react-native/docs/linking.html that it is doable to use geo: to link to map.

2 Answers 2

20

Use correct URL scheme

Have a look at the official documentation of iOS. To open maps, use the suggested scheme:

http://maps.apple.com/?ll=<lat>,<long>
4
  • Thanks a lot for the help :)
    – Kelvin
    Commented Mar 13, 2016 at 12:34
  • OK, but why linking with geo URL doesn't work? As @KelvinAliyanto mentioned, it's clearly written in React Native documentation. I expected, that when I use geo URL a menu pops up (if there are more than 1 map application), something like this: kevinandamanda.com/whatsnew/wp-content/uploads/2014/01/… Commented Oct 8, 2016 at 16:37
  • 1
    Follow the link to the official documentation of iOS. It says: "Unlike some schemes, map URLs do not start with a “maps” scheme identifier. Instead, map links are specified as regular http links and are opened either in Safari or the Maps app on the target platform."
    – purii
    Commented Oct 10, 2016 at 10:13
  • 5
    If you want to start a navigation, use http://maps.apple.com/?daddr=<lat>,<long>. Commented Apr 5, 2017 at 8:35
-2

You can use react-native-open-maps package

  1. Install the repository $ npm install --save react-native-open-maps

  2. Add an import to the top of your file import openMap from 'react-native-open-maps'

  3. Put it all together

import React, { Component } from 'react'
import { Button } from 'react-native'
import openMap from 'react-native-open-maps'

export default class App extends Component {
  _goToYosemite() {
    openMap({ latitude: 37.865101, longitude: -119.538330 });
  }

render() {
    return (
      <Button
        color={'#bdc3c7'}
        onPress={this._goToYosemite}
        title="Click To Open Maps 🗺" />
    );
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.