Reputation: 177
I'm trying to figure out how to retrieve the marker position when the marker is dragged. I found this : Drag marker event with callback providing lat/long?
and implemented on my app like so:
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>
);
}
}
the onMarkerDragEnd functions logs an evt object like this:
{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
I tried doing something like this:
onMarkerDragEnd = (evt) => {
console.log(evt.google.maps.Marker.getPosition().lat());
}
but it return can't get position of undefined
So how do i get the position of the marker from this returned event? Thank you for your help!
Upvotes: 9
Views: 22300
Reputation: 4462
centerMoved(mapProps, map) {
console.log('args:', mapProps, map);
console.log(map.getCenter().lat());
}
Upvotes: 0
Reputation: 792
Above answers not worked for me because onDragEnd
returning undefined
in my case.
onDragend={(t, map, coord) => this.onMarkerDragEnd(coord, index)}
So I find coordinates
by using below line
onDragEnd={(e) => this.onMarkerDragEnd(e.nativeEvent.coordinate)}
You can find it here (Draggable Markers)
https://github.com/react-native-community/react-native-maps
Simple Example
State
state = {
region: {
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
}
Marker
<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,
});
};
Hope, it will help you.
Upvotes: 5
Reputation: 796
Complete Solution
getLocation() function initially get the current location and set on the map.
onMarkerDragEnd() function get the marker location when we drag to another place.
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);
Upvotes: 0
Reputation: 1379
i have a simple solution
<GoogleMapReact
bootstrapURLKeys={{ key: '' }}
defaultCenter={this.state.center}
defaultZoom={this.state.zoom}
yesIWantToUseGoogleMapApiInternals={true}
onClick = {this.changeMarkerPosition}
>
then in the function
changeMarkerPosition =(e)=>{
this.setState({
...this.state,
details:{
...this.state.details,
lat:e.lat,
lng:e.lng
}
})
}
this will automatically give the latest co-ordinates and u can move ur marker accordingly by binding it to state.
Upvotes: 2
Reputation: 112777
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>
);
}
}
Upvotes: 15
Reputation: 537
I just wanted to add that onDragEnd does not work for me. Instead I needed to use onDragend with a lowercase e for the marker event as in:
<Marker
position={marker.position}
draggable={true}
onDragend={(t, map, coord) => this.onMarkerDragEnd(coord, index)}
name={marker.name}
/>
Upvotes: 0