简体   繁体   English

使用Google Maps API v3重新加载地图上的点

[英]Reload points on a Map in using the Google Maps API v3

I'm trying to reload points on a map based on when someone clicks a button. 我正在尝试根据某人单击按钮的时间在地图上重新加载点。

I have a couple json objects saved in memory, "data" and "data2". 我在内存中保存了两个json对象“ data”和“ data2”。 The following code takes the "data2" JSON file and sets it equal to the "data" JSON file and then triggers a map reload event. 以下代码获取“ data2” JSON文件,并将其设置为等于“ data” JSON文件,然后触发地图重新加载事件。

   dataholder = data
    function myFunction() {
      data = dataholder
      window.data = data2;
      google.maps.event.trigger(map, 'resize');
    }

Currently, the map won't reload with the new set of data. 目前,地图不会重新加载新的数据集。 However, I am not getting any error messages. 但是,我没有收到任何错误消息。

There's no map reload event. 没有地图重新加载事件。 The resize event, when it comes from a window resize, will at most recalculate the map bounds and reproject the existing features (markers, polylines, polygons). 调整大小事件来自窗口调整大小时,最多将重新计算地图范围并重新投影现有要素(标记,折线,多边形)。 For this case in particular, I don't think that manually triggering the resize event will have any effect. 特别是对于这种情况,我认为手动触发resize事件不会产生任何效果。

In second place, you say you have two json files, but your code suggest you instead have two json objects already in memory (meaning you don't need to perform aditional requests to retrieve the value of data2). 其次,您说您有两个json文件,但是您的代码建议您在内存中已有两个json对象(这意味着您无需执行附加请求即可检索data2的值)。 The rest of the answer will assume that. 其余答案将假定为。

If your original collection of markers came from "data", which is a collection of LatLng pairs, and you want to replace the markers for the contents of "data2" the process would be: 如果您最初的标记集合来自“数据”(这是LatLng对的集合),并且您想用标记替换“数据2”的内容,则过程将是:

1.- Push the markers into an object where you can find them later on 1.-将标记推入对象中,以后可以找到它们

var markers=[];
for(onemarker in data) {
    var newmarker=new google.maps.Marker({map: map, position: new google.maps.LatLng({onemarker.lat, onemarker.lng}) });
    markers.push(newmarker);
}

2.- When you want to replace the markers, first purge the markers array 2.-当您要更换标记时,请先清除标记数组

while(markers.length) {
     var oldmarker=markers.pop();
     oldmarker.setMap(null);
};

3.- Fill the markers array with the data from the data2 object 3.-用data2对象中的数据填充标记数组

for(onemarker in data2) {
    var newmarker=new google.maps.Marker({map: map, position: new google.maps.LatLng({onemarker.lat, onemarker.lng}) });
    markers.push(newmarker);
}

For this kind of markers, as for the regular features, there's no instantaneous binding between the object and the underlying data. 对于这种标记,对于常规功能,对象与基础数据之间没有即时绑定。 This is slightly different for the new google.maps.Data() layer for which you can skip the iteration and just feed it a geoJSON array. 对于新的google.maps.Data()层,这略有不同,您可以跳过该层,仅将其提供给geoJSON数组。 You still need to have an object to store the current markers, or else you won't have a way to access them when you want to remove them. 您仍然需要一个对象来存储当前标记,否则,当您要删除它们时将无法访问它们。

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

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