gpt4 book ai didi

reactjs - React Native MapBox 获取点击位置

转载 作者:行者123 更新时间:2023-12-01 21:32:33 29 4
gpt4 key购买 nike

我成功地将@react-native-mapbox-gl/maps 添加到我的项目中。现在我不需要需要用户的当前位置,但我只需要获取用户在 map 上单击的位置的坐标(纬度和经度)。我已经尝试了一切,但没有任何效果!我的代码:

import MapboxGL, {MapView, UserLocation} from '@react-native-mapbox-gl/maps';

MapboxGL.setAccessToken(
'MyAccessToken', );


export default class .... .... {

getLastLocation(location) {

ToastMaker(location, 'short')

}

render() {

return (
<View style={{flex: 1}}>
<View style={{height: '100%',
width: '100%',
backgroundColor: 'blue'}}>
<MapView style={{ flex: 1}}

//These Don't Work!
//onUserLocationUpdate={(property) => this.getLastLocation(property)}
//onLongPress={(property) => this.getLastLocation(property[0].coordinates)}
//onLongPress={(property) => this.getLastLocation(property.geometry) }
//onLongPress={(property) => ToastMaker(property.properties.screenPointY,'short') }
>
<MapboxGL.PointAnnotation
draggable={true}
id={'1'}
selected={true}
coordinate={this.state.coordinates}
/>
</MapView>
</View>
</View>
)
}

}

* 我只需要用户在 map 上选择的位置的坐标 谢谢 *

最佳答案

使用MapView#onPress属性(property)。

<MapView
...
onPress={(feature)=>console.log('Coords:', feature.geometry.coordinates)}
>
...
</MapView>

参见 ShowPointAnnotation.js完整的例子

已编辑:修复了函数中的变量错误“特征”

关于reactjs - React Native MapBox 获取点击位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62372010/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com