在 react-native 中单击地图视图上的任意位置时如何放置标记
How to place marker when click anywhere on the map view in react-native
我的要求是,当用户点击地图上的任何地方时,我需要在 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>
);
}
}
首先,为地图标记取一个空数组。
constructor(props) {
super(props)
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
markers: [] // Here it is
}
}
然后将点击位置的坐标作为新标记推入数组。最后,在您的 <MapView>
中,渲染所有标记:
<MapView style={styles.map} region={this.state.region}
onPress={(e) => this.setState({ markers: [...this.state.markers, { latlng: e.nativeEvent.coordinate }] })}>
{
// loop through markers array & render all markers
this.state.markers.map((marker, i) => (
<MapView.Marker coordinate={marker.latlng} key={i} />
))
}
</MapView>
每当您点击地图上的任意位置时,该位置的坐标将添加到 markers
数组中,并且随着 state
的更新,将再次调用 render()
函数并且所有标记都将被放置在包括新标记的地图上。
编辑
@FortuneCookie的评论:
To show only one marker on the map and when user taps on elsewhere the
other marker gets removed, just like dropping a pin.
简单多了。首先,将 markers
数组更改为单个 marker
in state.
constructor(props) {
super(props)
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
// markers: [] Change this
marker: null // to this
}
}
而对于 <MapView>
,只需更改其子项和 onPress
事件。
<MapView style={styles.map} region={this.state.region}
onPress={(e) => this.setState({ marker: e.nativeEvent.coordinate })}>
{
// if state contains marker variable with a valid value, render the marker
this.state.marker &&
<MapView.Marker coordinate={this.state.marker} />
}
</MapView>
您不必将多个标记放在数组中然后循环遍历它。相反,您只需将所选位置的坐标作为单个标记放入 state
,然后(如果存在标记)然后将其呈现在地图上。它会自动将所选位置设置为状态并删除之前选择的任何位置。
我的要求是,当用户点击地图上的任何地方时,我需要在 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>
);
}
}
首先,为地图标记取一个空数组。
constructor(props) {
super(props)
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
markers: [] // Here it is
}
}
然后将点击位置的坐标作为新标记推入数组。最后,在您的 <MapView>
中,渲染所有标记:
<MapView style={styles.map} region={this.state.region}
onPress={(e) => this.setState({ markers: [...this.state.markers, { latlng: e.nativeEvent.coordinate }] })}>
{
// loop through markers array & render all markers
this.state.markers.map((marker, i) => (
<MapView.Marker coordinate={marker.latlng} key={i} />
))
}
</MapView>
每当您点击地图上的任意位置时,该位置的坐标将添加到 markers
数组中,并且随着 state
的更新,将再次调用 render()
函数并且所有标记都将被放置在包括新标记的地图上。
编辑
@FortuneCookie的评论:
To show only one marker on the map and when user taps on elsewhere the other marker gets removed, just like dropping a pin.
简单多了。首先,将 markers
数组更改为单个 marker
in state.
constructor(props) {
super(props)
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
// markers: [] Change this
marker: null // to this
}
}
而对于 <MapView>
,只需更改其子项和 onPress
事件。
<MapView style={styles.map} region={this.state.region}
onPress={(e) => this.setState({ marker: e.nativeEvent.coordinate })}>
{
// if state contains marker variable with a valid value, render the marker
this.state.marker &&
<MapView.Marker coordinate={this.state.marker} />
}
</MapView>
您不必将多个标记放在数组中然后循环遍历它。相反,您只需将所选位置的坐标作为单个标记放入 state
,然后(如果存在标记)然后将其呈现在地图上。它会自动将所选位置设置为状态并删除之前选择的任何位置。