0

我使用“@react-native-mapbox-gl/maps”中的“import MapboxGL,{MarkerView}”获取地图。

      return (      
  <MarkerView coordinate={coordinate}  key={key}  onPress= {()=>console.log("pressed")}>   
      <View style={{
          height: 20, 
          width: 70, 
          backgroundColor: '#fff', 
         
          borderColor: '#fff', 
          borderWidth: 3
        }} 
      >
                                              
                <TouchableHighlight onPress= {()=>console.log("pressed")}>

                  <Text style={{color:"#000000"}} >{label}</Text>
                  </TouchableHighlight>

        </View>
       
  </MarkerView>

 );
 };

新闻事件在此处无法使用标记视图。

4

1 回答 1

1

实际上,MarkerView 上不存在 onPress 属性,请在此处检查

https://github.com/react-native-mapbox-gl/maps/blob/master/docs/MarkerView.md

代替 MarkerView 使用 MapboxGL.PointAnnotation ,在此您可以使用它的 onSelected 函数,如下所示:

<MapboxGL.MapView
     ref={(c) => (this._map = c)}>
         <MapboxGL.PointAnnotation
              onSelected={() => console.log("pressed")}
         >
            <Image source={require('./assets/marker.png')} />
         </MapboxGL.PointAnnotation>
</MapboxGL.MapView>

更多属性要求检查:

https://github.com/react-native-mapbox-gl/maps/blob/master/docs/PointAnnotation.md

于 2021-03-20T16:36:56.907 回答