This works beautifully on a computer, but I just tried testing it on mobile, and none of the onChildMouse
event handlers are recognized. I've tried finding other event handlers, but onChildMouseXXX
is the only ones I see for this package. How can I make the draggable marker work? Is there another event I can use, or a known work around to this issue?
import React from "react"
import PropTypes from "prop-types"
import GoogleMapReact from 'google-map-react';
const Marker = () => <div className="markerCircle"></div>;
class DraggableMap extends React.Component {
constructor(props) {
super(props);
this.state = {
center: {
lat: 0,
lng: 0
},
zoom: 11,
lat: 0,
lng: 0,
draggable: true,
loaded: false
}
this.onChildMouseMove = this.onChildMouseMove.bind(this)
this.onChildMouseUp = this.onChildMouseUp.bind(this)
this.onChildMouseDown = this.onChildMouseDown.bind(this)
}
componentDidMount() {
this.setState({
center: {
lat: this.props.lat,
lng: this.props.lng,
},
lat: this.props.lat,
lng: this.props.lng,
loaded: true
})
}
componentDidUpdate() {
if(this.props.lat != this.state.lat) {
this.setState({
center: {
lat: this.props.lat,
lng: this.props.lng,
},
lat: this.props.lat,
lng: this.props.lng,
loaded: true
})
}
}
onChildMouseDown(){
console.log('mouse down')
// set map no draggable
this.setState({
draggable: false,
});
}
onChildMouseUp(){
console.log('mouse up')
//set map draggable again
this.setState({
draggable: true,
});
}
onChildMouseMove(hoverKey, childProps, mouse){
console.log('move mouse')
// Change item data with new coordinates
// you need set here own store and update function
this.setState({
lat: mouse.lat,
lng: mouse.lng
}, () => this.props.updateLatLng(this.state.lat, this.state.lng))
}
render() {
if(!this.state.loaded) {
return null
}
return (
// Important! Always set the container height explicitly
<div style={{ height: '100%', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{ key: 'yesItIsMyRealKeyHere' }}
defaultCenter={this.state.center}
center={{lat: this.state.lat, lng: this.state.lng}}
defaultZoom={this.state.zoom}
draggable={this.state.draggable}
onChildMouseDown={() => this.onChildMouseDown()}
onChildMouseUp={() => this.onChildMouseUp()}
onChildMouseMove={(key, childProps, mouse) => this.onChildMouseMove(key, childProps, mouse)}
>
<Marker
key={'id'}
item={'n'}
lat={this.state.lat}
lng={this.state.lng}
/>
</GoogleMapReact>
</div>
);
}
}
export default DraggableMap;