Google Maps API v3 - Different markers/labels on different zoom levels

前端 未结 2 1609
情话喂你
情话喂你 2021-02-14 17:01

I was wondering if it is possible that Google has a feature to view different markers on different zoom levels.

For example, on zoom level 1, I want one marker over Chin

2条回答
  •  醉酒成梦
    2021-02-14 17:25

    The real reason why my program was failing was because of the Marker Manager, which doesn't work with my Markers with Labels.

    There was actually a much simpler approach, however, to fulfill my needs.

    function initialize() {
            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    
            getMarkers();
            google.maps.event.addListener(map,'zoom_changed',function () {
                 if (map.getZoom() >= 3) showMarkers();
                 if (map.getZoom() <= 2) eraseMarkers();
    
                });
    
        }
    
        function eraseMarkers() {
            for (i = 0; i < locations.length; i++) { 
                marker[i].setVisible(false);
            }
    
          }
    
        function showMarkers() {
            for (i = 0; i < locations.length; i++) { 
                marker[i].setVisible(true);
            }
    
          }
    
        function getMarkers() {
            for (i = 0; i < locations.length; i++) { 
                marker[i] = new MarkerWithLabel({
                  position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                  draggable: false,
                  map: map,
                  labelContent: locations[i][3],
                  labelAnchor: new google.maps.Point(30, 0),
                  labelClass: "labels", // the CSS class for the label
                  labelStyle: {opacity: 0.75}
                });
    
    
                }
            eraseMarkers();
          }
    

提交回复
热议问题