在React Native中链接Map

时间:2016-03-13 06:10:24

标签: ios reactjs react-native

我正在使用RN 0.20 for iOS。我想建立一个链接,如果您点击该链接,它会将您的坐标重定向到手机中可用的地图应用。这就是我使用链接组件所做的。

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));
}

但它一直让我“不知道怎么去”。但是,我从其https://facebook.github.io/react-native/docs/linking.html中的文档中看到,使用geo:链接到地图是可行的。

2 个答案:

答案 0 :(得分:15)

使用正确的URL方案

查看iOS的official documentation。 要打开地图,请使用建议的方案:

http://maps.apple.com/?ll=<lat>,<long>

答案 1 :(得分:-2)

您可以使用react-native-open-maps包

  1. 安装存储库 $ npm install --save react-native-open-maps

  2. 将导入添加到文件顶部 import openMap from 'react-native-open-maps'

  3. 全部放在一起

  4. 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 " />
        );
      }
    }