2017-08-06 126 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(); 
}