google-maps-react icon indicating copy to clipboard operation
google-maps-react copied to clipboard

HELP! Marker is not updating position.

Open AneudysOrtiz opened this issue 7 years ago • 10 comments

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)

AneudysOrtiz avatar Apr 06 '18 19:04 AneudysOrtiz

Please make a https://codesandbox.io/

https://github.com/fullstackreact/google-maps-react#issues

auser avatar Apr 07 '18 04:04 auser

Hi AneudysOrtiz,

You need to explicitly give it the lat and lng values. Just replace

<Marker position={this.state.position} name={'Current location'} />

with

<Marker position={{ lat: this.state.position.lat, lng: this.state.position.lng }} name={'Current location'} />

Hope this helps!

HMehmood1 avatar Apr 11 '18 14:04 HMehmood1

Thanks It worked!

AneudysOrtiz avatar Apr 13 '18 17:04 AneudysOrtiz

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

lieldulev avatar Jun 12 '18 23:06 lieldulev

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.

kashifaliquazi avatar Oct 09 '18 13:10 kashifaliquazi

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

kashifaliquazi avatar Oct 09 '18 13:10 kashifaliquazi

not working, tried with the {{ lat: this.state.position.lat, lng: this.state.position.lng }} but no luck. Any alternative?

rmpt avatar Feb 22 '20 21:02 rmpt

@rmpt yes it doesn't work for me as well

kannan007 avatar Apr 03 '20 12:04 kannan007

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?

roshanmaind avatar Jun 10 '20 10:06 roshanmaind

@HMehmood1 way didn't work for me but this did. Hope it helps you, i was desperate to find a solution.

martinvarelagarcia avatar Feb 12 '21 10:02 martinvarelagarcia