javascript 如何从 Google 地图数据层中删除数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23071775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove data from a Google Maps Data Layer?
提问by Rolando
I see Google Maps support geojson. Looking at the docs here: https://developers.google.com/maps/documentation/javascript/datalayer#load_geojson
我看到谷歌地图支持 geojson。查看此处的文档:https: //developers.google.com/maps/documentation/javascript/datalayer#load_geojson
Given the last example with the "Google", how can I make it such that I can click a button to add a new Geojson layer, and another button to toggle/remove the "Google" or even a letter? It seems to me that map.data appears to be a single layer, and is not multiple layers.
给定最后一个带有“Google”的例子,我怎样才能让我可以点击一个按钮来添加一个新的 Geojson 层,另一个按钮来切换/删除“Google”甚至一个字母?在我看来 map.data 似乎是一个单层,而不是多层。
回答by mensi
You are correct in that the Data Layer is a single layer. However, if you manually retrieve the GeoJSON and use the addGeoJson
function instead of loadGeoJson
you will get the added features returned. You can remove these later on.
您是正确的,因为数据层是一个单层。但是,如果您手动检索 GeoJSON 并使用该addGeoJson
函数而不是loadGeoJson
您将返回添加的功能。您可以稍后删除这些。
So instead of
所以代替
map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
You can do something like this (this example uses jQuery to get the data and assumes there is a button with the ID removeBtn):
您可以执行以下操作(此示例使用 jQuery 获取数据并假设有一个 ID 为 removeBtn 的按钮):
// Load the GeoJSON manually (works cross-origin since google sets the required HTTP headers)
$.getJSON('https://storage.googleapis.com/maps-devrel/google.json', function (data) {
var features = map.data.addGeoJson(data);
// Setup event handler to remove GeoJSON features
google.maps.event.addDomListener(document.getElementById('removeBtn'), 'click', function () {
for (var i = 0; i < features.length; i++)
map.data.remove(features[i]);
});
});
(See this JSbin for a working example you can play around with)
In more complex circumstances, you probably have to keep track of which datasource the user loaded and the features that got created because of that so you can delete them when requested.
在更复杂的情况下,您可能必须跟踪用户加载的数据源以及因此创建的功能,以便您可以在请求时删除它们。
回答by Irwin
This worked for me:
这对我有用:
map.data.forEach(function(feature) {
// filter...
map.data.remove(feature);
});
回答by jlivni
While map.data
is designed as a placeholder for the common case of a single datasource, you can have multiple, and still use addGeoJSon
using something like:
虽然map.data
被设计为单个数据源的常见情况的占位符,但您可以拥有多个,并且仍然使用以下内容addGeoJSon
:
// load data - do the same for data2, data3 or whatever
data1 = new google.maps.Data();
data1.loadGeoJson(url1);
// create some layer control logic for turning on data1
data1.setMap(map) // or restyle or whatever
// turn off data1 and turn on data2
data1.setMap(null) // hides it
data2.setMap(map) // displays data2
回答by Alexander van Oostenrijk
As a follow-up to @mensi's answer, it can be important to keep track of different sets of features loaded from different data sources. You could do that by adding a property to each feature:
作为@mensi 答案的后续行动,跟踪从不同数据源加载的不同功能集可能很重要。您可以通过为每个功能添加一个属性来做到这一点:
feature.setProperty('kind', 'region');
feature.setProperty('kind', 'lake');
However, it may be easier to create multiple data layers. A Google Map by default starts out with a single data layer, but you are not limited to that. You can do:
但是,创建多个数据层可能更容易。默认情况下,Google 地图从单个数据层开始,但您不仅限于此。你可以做:
var datalayer = new google.maps.Data({ map: mymap });
(It's important to set a map
option, or your data layer will not show up.)
(设置map
选项很重要,否则您的数据层将不会显示。)
This way, it's easier to group features by layer, and turn entire layers on or off.
通过这种方式,可以更轻松地按图层对要素进行分组,并可以更轻松地打开或关闭整个图层。