React Native MapBox获取点击位置

问题描述 投票:0回答:1

我成功地将@react-native-mapbox-glmaps添加到我的项目中。不要 需要用户的当前位置,但我只需要得到用户在地图上点击的任何地方的坐标(纬度& lng)。我已经试过了所有的方法,但没有任何效果!我的代码。

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

}

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

reactjs react-native google-maps mapbox openstreetmap
1个回答
1
投票

使用 MapView#onPress 财产。

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

ShowPointAnnotation.js 完整的例子

© www.soinside.com 2019 - 2024. All rights reserved.