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

How can I get all marker after it render?

Open samirbraga opened this issue 8 years ago • 3 comments

How can I get the Google Maps Marker object instances from a map?

samirbraga avatar Apr 21 '18 00:04 samirbraga

I may have a similar question. Did you figure it out perhaps?

natesct avatar May 16 '18 15:05 natesct

I tried this way to find the all markers, but I could not use Marker Component. Instead I create marker myself. I didn't find the way to use Marker Component and get all markers at the same time.

Here is the code.

<Map
  google={google}
  markers={markers}
  initialCenter={position}
  // ↓↓ use onReady
  onReady={this.onReady.bind(this)} />
onReady(mapProps, map) {
  const markerList= [];

  markers.forEach(marker => {
    const {anchorSize, scaledSize} = marker.icon;

    // ↓↓ create Marker myself
    const item = new google.maps.Marker({
      position: marker.position,
      map: map,
      name: marker.name,
      icon: {
        url: marker.icon.url,
        anchor: new google.maps.Point(anchorSize.width, anchorSize.height),
        scaledSize: new google.maps.Size(scaledSize.width, scaledSize.height)
      }
    });
    item.addListener('click', this.onMarkerClick.bind(this));
    markerList.push(item);
  }, this);

 
  this.setState({
    markerList: markerList
  });
}

If you add more options for marker, plz look at this document.

I hope you could fix this issue ;)

tidyline avatar Sep 06 '18 14:09 tidyline

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

feemcgill avatar Feb 06 '19 01:02 feemcgill