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

Map doesn't render markers on initial load and Cluster Markers disappearing.

Open NathHorrigan opened this issue 9 years ago • 5 comments

I am using the library to show markers from Mobx Data store but the markers don't show unless I have changed the source file and the UI is updated by HotModuleReplacement. Hopefully, I can get a solution as the library is Great!!

Here is my Map Comp:

@inject("map") @observer
export default class Map extends Component {

    constructor(props) {
        super(props);
    }

    render() {
        const mapStore = this.props.map

        const GettingStartedGoogleMap = withGoogleMap((props) => (
            <GoogleMap
                style={{
                    height: 100,
                    width: 100,
                }}
                onIdle={props.onIdle}
                onBoundsChanged={props.onBoundsChanged}
                defaultOptions={{ styles: this.mapStyles }}
                defaultZoom={mapStore.zoom}
                defaultCenter={{ lat: mapStore.center.lat, lng: mapStore.center.lng }}>
                {
                    props.markers.map((marker) => (
                        <Marker
                            position={marker}
                            icon={{
                                url: require('../../images/marker.png')
                            }}
                        />
                    ))
                }
            </GoogleMap>
        ))

        return (
            <GettingStartedGoogleMap
                style={{
                    height: 100,
                    width: 100,
                }}
                onIdle={() => {
                    mapStore.fetchMarkers()
                }}
                markers={this.props.map.markers}
                containerElement={
                    <div style={{ height: 'calc(100vh - 70px)' }
                    } />
                }
                mapElement={
                    <div style={{ height: 'calc(100vh - 70px)' }} />
                } />
        )
    }

    mapStyles = [MY MAPS STYLES]

}

Thanks in advance!

NathHorrigan avatar May 03 '17 18:05 NathHorrigan

Could you paste the marker object from your store ? 👍

MathieuCh avatar May 04 '17 20:05 MathieuCh

class MapState {
    @observable center
    @observable zoom
    @observable bounds
    @observable markers

    constructor() {
        this.center = {
            lng: -74.0117375,
            lat: 40.7157308,
        }
        this.zoom = 17
        // Marker Object:
        // {
        //  lat: somelat
        //  lng: somelng
        // }
        this.markers = []
    }

    @action setCenter(data) {
        this.center = data
    }

    @action setZoom(data) {
        this.zoom = data
    }

    @action setBounds(data) {
        this.bounds = data
    }

    @action onChange({ center, zoom, bounds }) {
        this.center = center
        this.zoom = zoom
        this.bounds = bounds
    }

    @action setMarkers(data) {
        this.markers = data
    }

    @action addMarker(data) {
        this.markers.push(data)
    }

    @action clearMarkers(data) {
        this.markers = []
    }

    @action fetchMarkers() {
        var params = {
            "ll": this.center.lat + "," + this.center.lng,
            "section": 'food',
            "radius": 3000,
        }
        foursquare.venues.explore(params)
            .then((res) => {
                res.response.groups[0].items.map((place) => {
                    if (this.markers.findIndex((x) => x.lat == place.venue.location.lat && x.lng == place.venue.location.lng) == -1) {
                        this.markers.push({
                            lat: place.venue.location.lat,
                            lng: place.venue.location.lng
                        })
                    }
                })
            });
    }

}

NathHorrigan avatar May 04 '17 21:05 NathHorrigan

Did you try adding a key to <Marker position={marker} icon={{ url: require('../../images/marker.png') }}

or passing {...marker} with spread operators Marker object = { position : { lat, lng }} i don't know why but it solved issues when i used it

MathieuCh avatar May 05 '17 06:05 MathieuCh

Issue turned out to be related to MobX and I had to do this:

<GettingStartedGoogleMap
                style={{
                    height: 100,
                    width: 100,
                }}
                onMapLoad={() => {
                    mapStore.fetchMarkers()
                }}
                onBoundsChanged={() => {
                    mapStore.fetchMarkers()
                }}
                markers={mobx.toJS(this.props.map.markers)} // <------ HERE
                containerElement={
                    <div style={{ height: 'calc(100vh - 70px)' }
                    } />
                }
                mapElement={
                    <div style={{ height: 'calc(100vh - 70px)' }} />
                } />

Now I'm having problem with ClusterMarkers, When I zoom in they disappear. Normal Markers work fine without ClusterMarkers.

NathHorrigan avatar May 05 '17 14:05 NathHorrigan

Problem was a MobX Issue, Closing

NathHorrigan avatar May 05 '17 17:05 NathHorrigan