question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HELP! Marker is not updating position.

See original GitHub issue

Hello, 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:open
  • Created 5 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
roshanmaindcommented, Jun 10, 2020

on changing the lat-long(within the state) of maker changing its position but it refreshes all the markers on the app and it is not efficient. can anyone tell me whats the right way to change the position.

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?

1reaction
lieldulevcommented, Jun 12, 2018

Wow this is a life saver. Someone should add this to the docs, thanks @HMehmood1 !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found