【问题标题】:Google Maps markerClusterer.MarkerClusterer not working with geoJSON谷歌地图 markerClusterer.MarkerClusterer 不使用 geoJSON
【发布时间】:2022-06-20 23:05:55
【问题描述】:

任何想法为什么我的标记没有聚集?我尝试了许多不同的方法,但没有什么能让它们聚集在一起。我意识到我传递 markerClusterer 的参数可能有问题,但我找不到让它与任何东西一起工作的方法。关于 markerClusterer.MarkerClusterer(使用 unpkg 时需要)的文档也很少甚至没有。

function initMap() {
    //set map options
    var myMapOptions = {
        center: {lat: 40.7498024, lng: -73.9774375},
        zoom: 12,  
    }
    
    //fill the html div with a map and pass in map options
    var myNewMap = new google.maps.Map(document.getElementById('mymap'), myMapOptions);
    
    //pass in data
    myNewMap.data.loadGeoJson('missedConnections.geojson');

    //define popup windows
    var infowindow = new google.maps.InfoWindow({maxWidth: 750, autopanMargin: 10,});
    
    //create new popup windows on marker click
    myNewMap.data.addListener('click', function(event) {
        console.log(event);
        // set variables
        let videourl = event.feature.getProperty('videoURL');
        //padding/margin is wonky on mobile vs desktop
        let html = '<video style="padding-bottom: 5px; padding-right: 3px;" preload="none" autoplay width=" 90%"><source src="' + videourl + '"></video>'; 
        // show the html variable in the infowindow
        infowindow.setContent(html); 
        infowindow.setPosition(event.latLng);
        infowindow.setOptions({pixelOffset: new google.maps.Size(0, -30)}); 
        // move the infowindow up 42 pixels to the top of the default marker icon
        infowindow.open(myNewMap);
    });

    new markerClusterer.MarkerClusterer({myNewMap.data, myNewMap});

}

【问题讨论】:

标签: javascript json google-maps google-maps-api-3 geojson


【解决方案1】:

首先,您使用的是object shorthand incorrectly

其次,这不是库的接口。

const markerCluster = new MarkerClusterer({ map, markers });

其中标记是google.maps.Markers 的数组。见MarkerClustererOptions

【讨论】:

  • 谢谢,但文档指出,当使用 unpkg 而不是 import 我应该使用 markerClusterer.MarkerClusterer ...不是这样吗?
  • 另外,我的标记不在数组中,它们在根据谷歌的方法加载的 json 文件中,没有办法将集群与 loadgeojson 一起使用吗?
  • 它需要 google.maps.Marker[] 而不是 geojson。您需要从 geojson 转换为标记
  • 这似乎不对,我无法想象没有办法使用geojson数据进行标记聚类...
  • jquery 只是解决方案的一小部分...$.each(data.features, callback) 变为 data.features.forEach(callback)
【解决方案2】:

进入你的html:

<script src="https://unpkg.com/@googlemaps/markerclusterer/dist/index.min.js"></script>

在你的 js 中,编写这两个代码

var mc = new markerClusterer.MarkerClusterer({ markers, map });

new markerClusterer.MarkerClusterer({ markers, map });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2020-03-28
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多