Changing icon based on zoom level

前端 未结 3 989
无人共我
无人共我 2020-12-19 03:52

How do I go about changing an icon height & width based on the Google Maps zoom level?

I\'m using Google Maps api v3.

3条回答
  •  半阙折子戏
    2020-12-19 04:27

    This is the code I used eventually:

    google.maps.event.addListener(google_map, 'zoom_changed', function() {
        var z = google_map.getZoom();
    
        _.each(map_shapes, function(s) {
    
            if (! $.isFunction(s.shape.getPosition)) return;
    
            var w = s.shape.getIcon().size.width;
            var h = s.shape.getIcon().size.height;
    
            s.shape.setIcon(new google.maps.MarkerImage(
                s.shape.getIcon().url, null, null, null, new google.maps.Size(
                    w - Math.round(w / 3 * (last_zoom - z)),
                    h - Math.round(h / 3 * (last_zoom - z)))
                )
            );
    
        });
    
        last_zoom = z;
    });
    

提交回复
热议问题