Showing sets of markers on different layers of Google map

ぃ、小莉子 提交于 2019-12-04 11:50:05

The Maps-API doesn't support this kind of custom layers(as you maybe know them from other map-API's like e.g. leaflet).

But it's not hard to achieve a similar feature.

You may use a google.maps.MVCObject. for every "layer" create a property for this MVCObject and set the value of this property to null or the google.maps.Map-instance( depending on the desired initial state of the "layer")

var myLayers=new google.maps.MVCObject();
    myLayers.setValues({parks:null,shops:null,hotels:map});
    //hotels initially are visible

When you want to add a Overlay...e.g. a Marker, to a "layer", bind the map-property of that Overlay to the related property of the MVCObject:

   parkMarker=new google.maps.Marker({/*options*/});
   parkMarker.bindTo('map',myLayers,'parks');

To toggle the display of all features within that "layer" you only need to set the property of the MVCObject:

//show the parks
myLayers.set('parks',map);
//hide the hotels
myLayers.set('hotels',null);

Demo: http://jsfiddle.net/doktormolle/UA85N/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!