Javascript 尝试使用@react google maps从数组中聚类标记时,this.props.children不是函数

Javascript 尝试使用@react google maps从数组中聚类标记时,this.props.children不是函数,javascript,reactjs,react-google-maps,Javascript,Reactjs,React Google Maps,我试图获取一组具有lat/long点的对象,并为每个对象创建一个标记,然后将这些标记传递到组件中 通过直接复制文档,我可以使其正常工作。然而,他们的方法与我需要的有点不同 区别似乎在于点如何映射到组件 工作代码: <MarkerClusterer options={{imagePath:"https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m"}} &g

我试图获取一组具有lat/long点的对象,并为每个对象创建一个标记,然后将这些标记传递到组件中

通过直接复制文档,我可以使其正常工作。然而,他们的方法与我需要的有点不同

区别似乎在于点如何映射到组件

工作代码:

<MarkerClusterer
      options={{imagePath:"https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m"}}
    >
      {
        (clusterer) => [
          {lat: -31.563910, lng: 147.154312},
          {lat: -33.718234, lng: 150.363181},
          {lat: -33.727111, lng: 150.371124},
          {lat: -33.848588, lng: 151.209834}
        ].map((location, i) => (
          <Marker
            key={i}
            position={location}
            clusterer={clusterer}
          />
        ))
      }
    </MarkerClusterer>
我的非工作代码:

const listings = [
    { lat: -31.56391, lng: 147.154312 },
    { lat: -33.718234, lng: 150.363181 },
    { lat: -33.727111, lng: 150.371124 },
    { lat: -33.848588, lng: 151.209834 },
  ];
  let testArray = [];
  for (let i = 0; i < listings.length; i++) {
    let location = listings[i];
    testArray.push(
      <Pin position={location} id={i} key={i} clusterer={listings} />
    );
  }
...

 <MarkerClusterer>
    {testArray}
 </MarkerClusterer>
下面是一个例子。代码在Map2.js下,差异在第61行

我不知道第一种方法在做什么,而我不是。也许是clusterer的参考资料?在此领域的任何帮助都将不胜感激。

该组件需要一个功能作为其子道具。该函数将由组件的渲染函数调用。在第二种情况下,将数组作为子道具传递。因此,MarkerClusterer试图将数组作为函数testArray调用,因此失败

这应该行得通

<MarkerClusterer
  options={{
    imagePath:
      "https://developers.google.com..."
  }}
>
  {clusterer =>
    listings.map((location, i) => (
      <Pin key={i} position={location} clusterer={clusterer} />
    ))
  }
</MarkerClusterer>

这在react世界中称为渲染道具。阅读了解更多详细信息。

@colemars此答案有帮助吗?