You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mapbox Cluster缩放时无法拆分为单个标记问题求助

问题:Mapbox集群缩放后无法拆分为单个标记

我使用react-map-gl实现Mapbox集群功能,目前能显示集群内的标记数量,但缩放地图时,集群无法拆分为单个标记展示。使用的React Mapbox库为visgl/react-map-gl。

以下是相关代码:

useEffect(() => {
const points = alleArtikel.map((artikel) => ({
  type: "Feature",
  properties: {
    cluster: false,
    artikelId: artikel._id,
    name: artikel.name,
    beschreibung: artikel.beschreibung,
    lng: artikel.long,
    lat: artikel.lat,
    bild: artikel.bild,
    userId: artikel.userId,
  },
  geometry: {
    type: "Point",
    coordinates: [parseFloat(artikel.long), parseFloat(artikel.lat)],
  },
}));
setPoints(points);
}, [alleArtikel]);

useEffect(() => {
supercluster.load(points);
setClusters(supercluster.getClusters(bounds, zoom));
}, [points, zoom, bounds]);

useEffect(() => {
if (mapRef.current) {
  setBounds(mapRef.current.getMap().getBounds().toArray().flat());
}
}, [mapRef?.current]);


         {clusters?.map((artikel) => {
          const { cluster: isCluster, point_count } = artikel.properties;
          const [longitude, latitude] = artikel.geometry.coordinates;
          if (isCluster) {
            return (
              <Marker
                key={artikel.id}
                longitude={longitude}
                latitude={latitude}
                onClick={() => setShowPopup(true)}
              >
                <div
                  className="cluster-marker"
                  style={{
                    width: `${13 + (point_count / points.length) * 20}px`,
                    height: `${13 + (point_count / points.length) * 20}px`,
                  }}
                  onClick={() => {
                    const zoom = Math.min(
                      supercluster.getClusterExpansionZoom(artikel.id),
                      10
                    );
                    mapRef?.current.flyTo({
                      center: [longitude, latitude],
                      zoom,
                      speed: 1,
                    });
                  }}
                >
                  {point_count}
                </div>
              </Marker>
            );
          }

          return (
            <Marker
              key={artikel.properties.artikelId}
              longitude={longitude}
              latitude={latitude}
            >
              <Box sx={{ position: "realtive" }}>
                <Tooltip title={artikel.properties.beschreibung}>
                  <img
                    src={artikel.properties.bild}
                    width={40}
                    height={40}
                  />
                </Tooltip>

                {showPopup && (
                  <Popup
                    closeOnClick={false}
                    closeButton={true}
                    longitude={longitude}
                    latitude={latitude}
                    anchor="bottom"
                    onClose={() => setShowPopup(false)}
                    style={{ position: "absolute", top: -40 }}
                  >
                    <Stack direction="column">
                      <Box>
                        <img
                          src={artikel.properties.bild}
                          style={{ maxWidth: 200 }}
                        ></img>
                      </Box>
                      <Box>
                        <Typography variant="body2" align="center">
                          {artikel.art}
                        </Typography>
                      </Box>
                      <Box>
                        <Typography variant="body1" align="center">
                          {artikel.name}
                        </Typography>
                      </Box>
                      <Box>
                        <Button variant="contained" fullWidth>
                          anfragen
                        </Button>
                      </Box>
                    </Stack>
                  </Popup>
                )}
              </Box>
            </Marker>
          );
        })}

地图初始加载状态:
初始加载的地图

即使放大到最大程度,集群仍未拆分:
放大后的地图

非常感谢您的帮助!

内容的提问来源于stack exchange,提问作者Captai-N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:36:41