删除数据层的所有功能 [英] Remove all features from data layer

查看:76
本文介绍了删除数据层的所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了类似于:

I used something like:

var map;
function initialize() {
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 4,
    center: {lat: -28, lng: 137.883}
  });
  map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
}

google.maps.event.addDomListener(window, 'load', initialize);

将geojson形状文件加载到我的地图的map.data图层。在形状文件中,有几个特征类定义要在地图上绘制的多边形。直到这里,我没有任何问题。

to load a geojson shape file to the map.data layer of my map. In the shape file, there are a couple of 'feature' classes defining polygons to be drawn on the map. Up until here I have no problems.

稍后,我想在另一个地方加载另一个geojson文件(替换地图上绘制的特征)。当你只是通过另一个文件加载另一个文件时,它只是通过另一个文件重新绘制它。在新的geojson形状文件中加载之前,如何清除所有功能的map.data层?

Later on though, I want to load another geojson file over the other one (replacing the drawn 'features' on the map). When you just load another file over the other one it just redraws it over the other one. How on earth do you clear the map.data layer of all the features before loading in the new geojson shape file?

我试过使用
map.data.remove(feature)
带有一个循环,但我似乎无法从map.data图层获取所有功能。

I've tried using map.data.remove(feature) with a loop, but I can't seem to get all the features from the map.data layer.

推荐答案

这将迭代所有功能并将其从 map.data 中删除。

This will iterate over all the features and remove them from map.data.

map.data.forEach(function(feature) {
    // If you want, check here for some constraints.
    map.data.remove(feature);
});

编辑1:解释
每个函数的地图数据使用回调,所以你必须给一个回调函数作为参数:

Edit 1: Explanation Map data forEach function use callbacks, so you have to give a callback function as parameter:

var callback = function(){ alert("Hi, I am a callback"); }; 
map.data.forEach(callback);

现在,数据中的每个元素都会显示警报。

Now for each element in data it will show an alert. It's also possible to give callbacks with parameter, like in the code shown above.

   var callback = function(feature) {
        // If you want, check here for some constraints.
        map.data.remove(feature);
   };
   map.data.forEach(callback);

更多解释和示例: http://recurial.com/programming/understanding-callback-functions-in-javascript/

这篇关于删除数据层的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆