React Native MapBox 获取点击位置

mr *_*eek 2 google-maps openstreetmap mapbox reactjs react-native

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

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

}
Run Code Online (Sandbox Code Playgroud)

* 我需要的只是用户在地图上选择的坐标 谢谢 *

mfa*_*kas 7

使用MapView#onPress财产。

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

有关完整示例,请参阅ShowPointAnnotation.js

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

  • 人们说消防员是真正的英雄,而我们在 Stackoverflow 上就有这样的英雄 :D (3认同)