Google Maps GeoJSON-切换标记层吗? [英] Google maps GeoJSON- toggle marker layers?

查看:114
本文介绍了Google Maps GeoJSON-切换标记层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从调用PostGIS数据库返回了一些GeoJSON.我希望能够为每个功能添加一个标记,并能够切换不同类型的标记/功能.目前,我正在使用JavaScript为每个功能生成标记,然后根据类型将它们添加到数组中,然后根据需要对数组设置show/hide进行切换,以切换图层".

I have some GeoJSON returned from a call to a PostGIS database. I'd like to be able to add a marker for each feature, and be able to toggle different types of marker/feature. Currently I'm using JavaScript to generate a marker for each feature, adding them to arrays according to type, and then going through the arrays setting show/hide as appropriate to toggle the 'layers'.

这可以正常工作,但是我想知道新的GeoJSON功能是否提供了更好的方法.据我所知,所有功能都添加到同一数据层,并且切换它们的集合将涉及设置样式或仅替换为新的,预先过滤的GeoJSON.

This works OK, but I'm wondering if the new GeoJSON functionality offers a better way to do this. As far as I can see though, all the features get added to the same datalayer and toggling sets of them would involve either setting styles or just replacing with new, pre-filtered GeoJSON.

所以问题是,是否可以有多个数据层,并轻松地从地图中添加/删除它们?还是我最好看看OpenLayers之类的东西?

So the question is is it possible to have more than one data layer, and easily add/remove them from the map or am I better off looking at something like OpenLayers?

更多研究表明这很简单.

Bit more research shows it's quite straightforward.

对于我们要打开的要素集中的每种要素类型,创建一个新的Data对象.将所有相关功能添加到该数据对象.

For each type of feature in the feature collection that we want to toggle on, create a new Data object. Add all the relevant features to that data object.

var datalayer = new google.maps.Data();
datalayer.addGeoJson(feature);
datalayer.setMap(mainmap);

然后将每个数据对象/功能类型存储为键值对.进行切换时,请拉出相关的数据对象并根据需要设置setMap:

Then store each data object/feature type as a key-value pair. On toggle, pull out the relevant data object and setMap as appropriate:

var datalayer= featuretypesobj["feature type to toggle"];
datalayer.setMap(mymap); //or
datalayer.setMap(null);

推荐答案

您还可以创建单独的图层

You can also create separate layers

var layer_1 = new google.maps.Data();
var layer_2 = new google.maps.Data();

然后填充它,例如带有json数据

then populate it, e.g. with json data

layer_1.loadGeoJson('/path/to/data.json');
layer_2.loadGeoJson('/path/to/data2.json');

然后在地图上添加/删除它们

then add / remove them on the map

layer_1.setMap(map);
layer_2.setMap(map);
layer_1.setMap(null);

这篇关于Google Maps GeoJSON-切换标记层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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