2017-08-06 73 views
2

我想让我的按钮onPress功能来调用一个函数,将打开苹果地图或谷歌地图取决于设备。出于某种原因,当我按下按钮时没有任何事情发生。反应本机开放本地地图

这里是我的功能:

openMap=() => { 
    console.log('open directions') 
    Platform.select({ 
     ios:() => { 
      Linking.openURL('http://maps.apple.com/maps?daddr='); 
     }, 
     android:() => { 
      Linking.openURL('http://maps.google.com/maps?daddr='); 
     } 
    }); 
} 

下面是按钮:

<TouchableOpacity 
      onPress={this.openMap} 
      style={styles.navigateBtn}> 
      <Icon name="md-navigate" style={{ fontSize: 24 }} /> 
      <Text 
       style={{ fontSize: 13, fontWeight: "700", lineHeight: 14 }} 
      > 
       NAVIGATE 
       </Text> 
      </TouchableOpacity> 

最后,我想通过经度和纬度进入openMap函数来获取行车路线,但首先我需要获得地图打开。

这是我进口

import { View, TouchableOpacity, Modal, Platform, Alert, StyleSheet, Linking } from "react-native"; 
import {Header, Content, Text, Button, Icon, Card,CardItem, Title, Left, Right, Body, Container 
} from "native-base"; 
import { Constants } from 'expo 

回答

1

您的按钮似乎在呼吁this.MapTouchableOpacityonPress。它不应该是this.openMap

希望它能帮助你!

编辑:

试图声明你的函数你这样的组件内:

openMap() { 
    console.log('open directions') 
    Platform.select({ 
     ios:() => { 
      Linking.openURL('http://maps.apple.com/maps?daddr='); 
     }, 
     android:() => { 
      Linking.openURL('http://maps.google.com/maps?daddr='); 
     } 
    }); 
} 

并为您的TouchableOpacity试试这个

<TouchableOpacity 
     onPress={() => this.openMap() } 
     style={styles.navigateBtn}> 
     <Icon name="md-navigate" style={{ fontSize: 24 }} /> 
     <Text 
      style={{ fontSize: 13, fontWeight: "700", lineHeight: 14 }} 
     > 
+0

@查尔斯 - 奥利弗·德默斯那是一个错字opps!接得好。在我的代码中,“onPress = {this.openMap}”仍然不工作:( –

+0

你的openMap被调用了吗?你看到你的console.log吗? –

+0

试试看我的新答案 –

0
openMap=() => { 
    console.log('open directions') 
    let f = Platform.select({ 
     ios:() => { 
      Linking.openURL('http://maps.apple.com/maps?daddr=38.7875851,-9.3906089'); 
     }, 
     android:() => { 
      console.log('ANDROID') 
      Linking.openURL('http://maps.google.com/maps?daddr=38.7875851,-9.3906089').catch(err => console.error('An error occurred', err));; 
     } 
    }); 

    f(); 
}