首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >googlemaps反应包装-标记群集创建@googlemaps/ React包装器

googlemaps反应包装-标记群集创建@googlemaps/ React包装器
EN

Stack Overflow用户
提问于 2022-10-10 05:38:09
回答 1查看 218关注 0票数 2

我使用Google ReactJS库将地图添加到我的React应用程序中,并使用@googlemaps/react库来聚类标记。但我无法在包装器上进行标记聚类。如果有人知道,请帮我解决这个问题。组件代码如下:

代码语言:javascript
复制
import React, { useEffect, useRef } from "react";
import { Wrapper, Status } from "@googlemaps/react-wrapper";

export default function MapContainer({ center, zoomLevel, markerList, icon }) {

    const googleMapRef = useRef(null);
    let googleMap = null;

    useEffect(() => {
        try {
            let bounds = new window.google.maps.LatLngBounds();

            const initGoogleMap = () => {
                return new window.google.maps.Map(googleMapRef.current, {
                    center: center,
                    zoom: zoomLevel,
                    styles: [{ stylers: [{ saturation: -100 }] }]
                });
            }

            const createMarker = (markerObj) => new window.google.maps.Marker({
                position: { lat: parseFloat(markerObj.lat), lng: parseFloat(markerObj.lng) },
                map: googleMap,
                icon: {
                    url: icon,
                    scaledSize: new window.google.maps.Size(80, 80)
                },
            });

            googleMap = initGoogleMap();

            markerList.map(x => {
                const marker = createMarker(x);
                bounds.extend(marker.position);
            });

            if (markerList.length === 0) {
                initGoogleMap();
                bounds = new window.google.maps.LatLngBounds();
            }
            googleMap.fitBounds(bounds);
        }
        catch (e) {
            console.log("maps", e)
        }
    })

    const render = (status) => {
        if (status === Status.LOADING) return "Loading...";
        if (status === Status.FAILURE) return "Error";
        return null;
    };

    return (
        <div>
            <div>
                <Wrapper apiKey={TOKEN} render={render}>
                    {/* <GISTrackingMap zoomLevel={props.zoomLevel} mapFilterByVal={props.mapFilterByVal} center={props.center} gisTrackingData={props.gisTrackingData} /> */}
                    <div ref={googleMapRef} style={{ width: "100%", height: "78vh" }} />
                </Wrapper>
            </div>
        </div>
    )
}
EN

回答 1

Stack Overflow用户

发布于 2022-10-20 22:55:37

要创建一个集群器,必须有一系列标记。因此,我建议将bounds.extend(marker.position);从markerList映射中移出并保存结果:

代码语言:javascript
复制
const markers = markerList.map(x => {
  createMarker(x);
});

然后创建一个集群:

代码语言:javascript
复制
new MarkerClusterer({ googleMapRef, markers });

UPD看起来MarkerClusterer不适用于ref,因此上面的代码应该按如下方式更改:

代码语言:javascript
复制
new MarkerClusterer({ googleMap, markers });

不要忘记安装库并添加导入:

代码语言:javascript
复制
yarn add @googlemaps/markerclusterer
代码语言:javascript
复制
import { MarkerClusterer } from '@googlemaps/markerclusterer';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74010596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档