我正在尝试找出在拖动标记时如何检索标记位置的方法。我发现了:Drag marker event with callback providing lat/long?
并在我的应用中实现,如下所示:
export class MapContainer extends React.Component {
onMarkerDragEnd = evt => {
console.log(evt);
};
render() {
const style = {
width: "100%",
height: "300px"
};
let lat = this.props.lat;
let lng = this.props.lng;
return (
<Map
google={this.props.google}
style={style}
initialCenter={{
lat: lat,
lng: lng
}}
zoom={14}
>
<Marker
onClick={this.onMarkerClick}
draggable={true}
onDragend={this.onMarkerDragEnd}
name={"Current location"}
/>
</Map>
);
}
}
onMarkerDragEnd函数将这样记录一个evt对象:
{onClick: undefined, draggable: true, onDragend: ƒ, name:
"Currentlocation", map: Zf, …}
draggable: true
google:{maps: {…}}
map: Zf {gm_bindings_: {…}, __gm: uf, gm_accessors_: {…}, mapTypeId:
"roadmap", center: _.K, …}
mapCenter:{lat: "37.122024", lng: "25.234458"}
name:"Current location"
onClick:undefined
onDragend:ƒ (evt)
__proto__:Object
我试图做这样的事情:
onMarkerDragEnd = (evt) => {
console.log(evt.google.maps.Marker.getPosition().lat());
}
但是返回无法获得未定义的位置
那么我如何从返回的事件中获取标记的位置? 谢谢您的帮助!
答案 0 :(得分:5)
The third argument to the onDragend
event listener function contains the coordinates of the marker after the drag.
Example
class MapContainer extends React.Component {
state = {
markers: [
{
name: "Current position",
position: {
lat: 37.77,
lng: -122.42
}
}
]
};
onMarkerDragEnd = (coord, index) => {
const { latLng } = coord;
const lat = latLng.lat();
const lng = latLng.lng();
this.setState(prevState => {
const markers = [...this.state.markers];
markers[index] = { ...markers[index], position: { lat, lng } };
return { markers };
});
};
render() {
return (
<Map
google={this.props.google}
style={{
width: "100%",
height: "300px"
}}
zoom={14}
>
{this.state.markers.map((marker, index) => (
<Marker
position={marker.position}
draggable={true}
onDragend={(t, map, coord) => this.onMarkerDragEnd(coord, index)}
name={marker.name}
/>
))}
</Map>
);
}
}
答案 1 :(得分:3)
以上答案对我不起作用,因为在我的情况下onDragEnd
返回了undefined
。
onDragend={(t, map, coord) => this.onMarkerDragEnd(coord, index)}
所以我在下面的行中找到了coordinates
onDragEnd={(e) => this.onMarkerDragEnd(e.nativeEvent.coordinate)}
您可以在此处找到它(可拖动标记)
https://github.com/react-native-community/react-native-maps
简单示例
州
state = {
region: {
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
}
标记
<Marker
coordinate={this.getMapRegion()}
draggable={true}
onDragEnd={(e) => this.onMarkerDragEnd(e.nativeEvent.coordinate)}
title={ "Location"}
/>
onMarkerDragEnd
onMarkerDragEnd = (coord) => {
let newRegion = {
latitude: parseFloat(coord.latitude),
longitude: parseFloat(coord.longitude),
latitudeDelta: 0.0522,
longitudeDelta: 0.0321,
};
this.setState({
region: newRegion,
});
};
希望,它将为您提供帮助。
答案 2 :(得分:1)
我有一个简单的解决方案
<GoogleMapReact
bootstrapURLKeys={{ key: '' }}
defaultCenter={this.state.center}
defaultZoom={this.state.zoom}
yesIWantToUseGoogleMapApiInternals={true}
onClick = {this.changeMarkerPosition}
>
然后在功能中
changeMarkerPosition =(e)=>{
this.setState({
...this.state,
details:{
...this.state.details,
lat:e.lat,
lng:e.lng
}
})
}
这将自动提供最新的坐标,并且您可以通过将其绑定到状态来相应地移动ur标记。
答案 3 :(得分:0)
我只是想补充一点,onDragEnd对我不起作用。相反,我需要对标记事件使用onDragend和小写字母e,如下所示:
<Marker
position={marker.position}
draggable={true}
onDragend={(t, map, coord) => this.onMarkerDragEnd(coord, index)}
name={marker.name}
/>
答案 4 :(得分:0)
完整的解决方案
getLocation()函数最初会获取当前位置并在地图上进行设置。
onMarkerDragEnd()函数可在我们拖动到另一个位置时获得标记位置。
import React, { Component } from 'react';
import { Map, GoogleApiWrapper, Marker } from 'google-maps-react';
class Maps extends Component {
constructor(props) {
super(props);
this.state = {
markers: [
{
name: "Current position",
position: {
lat: '24.914161699999998',
lng: '67.082216'
}
}
]
}
}
onMarkerDragEnd = (coord, index) => {
const { latLng } = coord;
const lat = latLng.lat();
const lng = latLng.lng();
this.setState(prevState => {
const markers = [...this.state.markers];
markers[index] = { ...markers[index], position: { lat, lng } };
return { markers };
});
}
getLocation = () => {
if (navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(pos => {
const coords = pos.coords;
let newState = Object.assign({}, this.state);
newState.markers[0].position.lat = coords.latitude;
newState.markers[0].position.lng = coords.longitude;
this.setState(newState);
console.log("map", this.state.markers[0].position.lat, this.state.markers[0].position.lng)
});
}
}
componentDidMount() {
this.getLocation()
}
handleSubmit = async e => {
e.preventDefault();
const location = {
latitude: this.state.markers[0].position.lat,
longitude: this.state.markers[0].position.lng
}
}
render() {
return (
<div>
<Map
google={this.props.google}
style={{
width: "100%",
height: "300px"
}}
zoom={14}
initialCenter={{ lat: this.state.markers[0].position.lat, lng: this.state.markers[0].position.lng }}
>
{this.state.markers.map((marker, index) => (
<Marker
key={index}
position={marker.position}
draggable={true}
onDragend={(t, map, coord) => this.onMarkerDragEnd(coord, index)}
name={marker.name}
/>
))}
</Map>
<button type="submit" onClick={this.handleSubmit} >submit</button>
</div>
);
}
}
export default GoogleApiWrapper({
apiKey: 'google api key here'
})(Maps);
答案 5 :(得分:0)
centerMoved(mapProps, map) {
console.log('args:', mapProps, map);
console.log(map.getCenter().lat());
}