HELP! Marker is not updating position.
See original GitHub issueHello, I’m trying to place the marker wherever I click on the map. I’m saving the current position in the state and passing it to the Marker component as props so It should change position when the state changes.
This is how I’m implementing it. But the marker doesn’t change position. BTW I’m using TSX. Hope you can help me.
import * as React from 'react';
import { Map, Marker, GoogleApiWrapper } from 'google-maps-react';
interface IMapState {
position: LatLng;
}
interface LatLng {
lat: number;
lng: number;
}
export class Mapa extends React.Component<{}, IMapState>{
constructor(props) {
super(props);
this.state = {
position: { lat: 18.486278764986732, lng: 69.92786525735443 }
}
this.onMapClicked = this.onMapClicked.bind(this);
}
onMapClicked(props, map, e) {
let location = this.state.position;
location.lat = e.latLng.lat();
location.lng = e.latLng.lng();
this.setState({
position: location
})
console.log(this.state.position);
}
render() {
return (
<Map google={(window as any).google} zoom={14} className={'map'} initialCenter={this.state.position} onClick={this.onMapClicked}>
<Marker position={this.state.position} name={'Current location'} />
</Map>
)
}
}
export default GoogleApiWrapper({
apiKey: ('AIzaSyDJKV1bs7RogqpcMvvSuSLTDPB19lPR5dI')
})(Mapa)
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
update map marker position onLocationChanged() is not ...
I'm working on google maps and I want to change map marker position when user location changed in onLocationChanged() method.
Read more >Leaflet - Update position of different markers on drag
Now, I want to make draggable these markers and get the new positions of them in some inputs. Unfortunately I don't understand how...
Read more >Markers | Maps SDK for Android - Google Developers
Markers indicate single locations on the map. You can customize your markers by changing the default color, or replacing the marker icon with...
Read more >Markers change position when the map is zoomed
Whilst this appears to be working fine, when the map is zoomed, the markers ... method but it's not working. can you pls...
Read more >moving the marker to current location after data gets updated??
if you click on the button it takes you back to your current location and updates the fields and moves the marker back....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Same here! I am updating my marker’s position by updating the state variable every 500 ms. But every time we update the position prop of the marker component, the marker re-renders. Meaning that the original marker get removed and then a new marker is rendered on the new position. This causes a “blinking”/“flickering” effect when the change in the position of the marker is too small or zero.
Here’s an example.
Can anyone please help with this so that the marker doesn’t entirely re-render but simply moves to the new location?
Wow this is a life saver. Someone should add this to the docs, thanks @HMehmood1 !