简体   繁体   English

从Google地图中删除标记簇[不仅仅是标记]

[英]Delete marker clusters from google maps [not just the markers]

In my phonegapp/cordova app, I use the google maps tool and I need to change, sometimes, the points showed on it. 在我的phonegapp / cordova应用程序中,我使用了Google Maps工具,有时需要更改其上显示的点。 I found some useful code here to remove my markers from the google maps I use. 我在这里找到了一些有用的代码可以从我使用的Google地图中删除标记。

My problem I also use marker clusters and I have a weird problem here. 我的问题我也使用标记簇,这里有一个奇怪的问题。

For my maps, I use these settings: 对于我的地图,我使用以下设置:

{
    showControls: true,
    key: { google: "XXXXXX" },
    center: center,
    width: "100%",
    height: "95%",
    zoom: zoom,
    provider: "google",
    mapType: "satellite",
    autoAdjust: false,
    onReady: SetMap
}

Where SetMap and the other called functions are: 其中SetMap和其他调用的函数是:

var mmm = null;
var markerCluster = null;

// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
    setMapOnAll(null);
}

// Sets the map on all markers in the array.
function setMapOnAll(map) {
    for (var i = 0; i < markerCluster.markers_.length; i++) {
        markerCluster.markers_[i].setMap(map);
    }
}

function SetMap (s) {

    if (mmm == null)
        mmm = s.originalMap;
    else {
        clearMarkers();
        markerCluster.markers_ = [];
    }
    var map = mmm;
    var markers = [];
    for (var i = 0; i < pointsOnMapList.length; i++) {
        var data = pointsOnMapList[i];
        var latLng = new google.maps.LatLng(data.location[0], data.location[1]);
        var marker = createMarker(latLng, data.title, map, data.idimp);
        markers.push(marker);
    }
    markerCluster = new MarkerClusterer(map, markers, { imagePath: 'images/m' });
}

When I update my dataset, I simply call the SetMap function. 更新数据集时,只需调用SetMap函数。

This "almost" work. 这项“差不多”的工作。 My goal is to change the points in the map after the user filters them. 我的目标是在用户过滤点后更改地图中的点。 I get a new dataset from my api and I have to erase the map and populate it again with my new points. 我从api中获得了一个新的数据集,我必须删除地图,并用新的点再次填充它。

Example: let's say I have this map: 例如:假设我有这张地图:

在此处输入图片说明

Then, I filter my map points and I get old and new points in the same maps: 然后,我过滤了地图点,并在同一张地图中得到了旧点和新点:

在此处输入图片说明

Then, like magic, if I just change the map zoom, the old points disappear! 然后,就像魔术一样,如果我仅更改地图缩放比例,旧点就会消失!

在此处输入图片说明

NOTE: the single markers are deleted as I filter my dataset, without changing the zoom. 注意:在我过滤数据集时,不会删除单个标记,而不会更改缩放比例。 This problem is only for the marker clusters. 此问题仅适用于标记簇。 How can I delete them? 如何删除它们?

Just found the clue. 刚找到线索。 Editing my code in this way: 以这种方式编辑代码:

function SetMap (s) {
    if (mmm == null)
        mmm = s.originalMap;
    else {
        markerCluster.clearMarkers();
    }
    var map = mmm;
    var markers = [];
    for (var i = 0; i < pointsOnMapList.length; i++) {
        var data = pointsOnMapList[i];
        var latLng = new google.maps.LatLng(data.location[0], data.location[1]);
        var marker = createMarker(latLng, data.title, map, data.idimp);
        markers.push(marker);
    }
    markerCluster = new MarkerClusterer(map, markers, { imagePath: 'images/m' });
}

it works, all the markers and cluster are removed!!! 它可以工作,所有标记和群集都将被删除!!!

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

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