MaoView
我的要求是,当用户单击地图上的任意位置时,我需要在 上显示标记,并且还需要获取放置标记的位置的坐标(纬度和经度)。
这是我尝试过的:
class Maps extends React.Component {
constructor(props) {
super(props);
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
marker: {
latlng: {
latitude: 17.6868,
longitude: 83.2185,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
}
};
}
onMapPress(e) {
alert("coordinates:" + JSON.stringify(e.nativeEvent.coordinate));
this.setState({
marker: [
{
coordinate: e.nativeEvent.coordinate
}
]
});
}
handleMarkerPress(event) {
const markerID = event.nativeEvent.identifier;
alert(markerID);
}
render() {
return (
<MapView
identifier={"1"}
ref={component => (this.map = component)}
provider={this.props.provider}
style={styles.map}
region={this.state.region}
onPress={this.onMapPress.bind(this)}
//onPress={(event) => this.onMapPress(event)}
provider={PROVIDER_DEFAULT}
mapType="standard"
zoomEnabled={true}
pitchEnabled={true}
showsUserLocation={true}
followsUserLocation={true}
showsCompass={true}
showsBuildings={true}
showsTraffic={true}
showsIndoors={true}
>
<MapView.Marker coordinate={this.state.marker.latlng} />
</MapView>
);
}
}