简体   繁体   English

无法在 google maps markerclusterer 中应用自定义图标进行聚类,因为无法提供 position 数据

[英]Unable to apply custom icons for clustering in google maps markerclusterer because unable to provide position data

The documentation for this utility make no sense to me.该实用程序的文档对我来说毫无意义。 According to this and this , you can customise cluster icons by first converting the cluster icon to a marker via the MarkerClusterer 's renderer property, and then apply icon styles as you would a normal marker.根据thisthis ,您可以通过首先通过MarkerClustererrenderer属性将集群图标转换为标记来自定义集群图标,然后像普通标记一样应用图标 styles。 However, a new google.maps.Marker requires a position property to work - a ``property I don't have access to.但是, new google.maps.Marker需要一个position属性才能工作——一个“我无权访问的属性”。 I have access to the individual positions of individual marker locations, but the whole point of using the marker clustering functionality is that is calculates a mid-point for you.我可以访问各个标记位置的各个位置,但使用标记聚类功能的全部意义在于为您计算一个中点。

How can I render the cluster in the correct position, if I don't know the position?如果我不知道 position,如何在正确的 position 中呈现集群? I can access a _position property on the stats prop, but that throws an error:我可以访问stats道具上的_position属性,但这会引发错误:

Cannot read properties of undefined (reading 'addListener')

I'm really lost as to what to do here, because there don't seem to be many reliable examples out there.我真的不知道在这里做什么,因为那里似乎没有很多可靠的例子。 As far as I can tell, I'm following the example set out in their github .据我所知,我正在按照他们的 github中列出的示例进行操作。

private createClusteredMarkerMap() {
  new this._GoogleMaps.load((google) => {
    let map = new google.maps.Map(this._map, mapOptions);
    const markers = this._locations.map(({ lat, long }) => {
      // loop and extract lat/lng
    });

    new markerClusterer.MarkerClusterer({
      map,
      markers,
      renderer: {
        render: (clusters, stats) => {this.testRenderer(clusters, stats)} // trying to edit the icons here
      }
    });
  });
}

private testRenderer(clusters, stats) {
  const svg = // create svg here
  return new google.maps.Marker({
// position is required here but I don't have that value
    icon: {
      url: `data:image/svg+xml;base64,${svg}`,
      scaledSize: new google.maps.Size(45, 45),
    },
    label: {
      text: String(stats.count),
      color: "rgba(255,255,255,0.9)",
      fontSize: "12px",
    },
  });
}
const renderer = {
  render({ count, position }) {
    return new google.maps.Marker({
      label: { text: String(count), color: "white", fontSize: "12px" },
      position,
      icon: "url to the file",
      // adjust zIndex to be above other markers
      zIndex: Number(google.maps.Marker.MAX_ZINDEX) + count,
    })
  }
}

// pass your function to the markerCluster constructor
const cluster = new MarkerClusterer({map, markers, renderer})

Missing a return statement on the render method.缺少有关render方法的return语句。 High fives all round.全场击掌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM