Javascript 缩小时防止标记缩放

Javascript 缩小时防止标记缩放,javascript,google-maps-api-3,google-maps-markers,Javascript,Google Maps Api 3,Google Maps Markers,我希望Google Maps标记在特定缩放级别上具有固定大小,如本例所示: 但是有没有比上面的Jsfiddle更平滑的解决方案呢?根据您的评论:是的,您可以用一个标记来解决它,但是标记选项中没有“随时可用”选项。您可以添加缩放更改侦听器,并为标记图标设置新的比例。而不是地面覆盖层 var icon = 'https://www.google.com/mapfiles/marker_black.png'; var iconBounds = constructBounds(38.713107, -9

我希望Google Maps标记在特定缩放级别上具有固定大小,如本例所示:


但是有没有比上面的Jsfiddle更平滑的解决方案呢?

根据您的评论:是的,您可以用一个标记来解决它,但是标记选项中没有“随时可用”选项。您可以添加缩放更改侦听器,并为标记图标设置新的比例。而不是地面覆盖层

var icon = 'https://www.google.com/mapfiles/marker_black.png';
var iconBounds = constructBounds(38.713107, -90.42984);
var staticOverlay = new google.maps.GroundOverlay(icon, iconBounds);
staticOverlay.setMap(map);
你可以用记号笔

var icon = 'https://www.google.com/mapfiles/marker_black.png';
var marker = new google.maps.Marker({
    position: {
        lat: 38.713107,
        lng: -90.42984
    },
    map: map,
    icon: icon
});
然后,您只需添加缩放更改侦听器,并根据缩放级别计算新的大小:

//add a zoom change listener, so you can resize the icon based on the zoom level
google.maps.event.addListener(map, 'zoom_changed', function() {

    var zoom = map.getZoom();
    markerWidth = (zoom/9)*20
    markerHeight = (zoom/9)*34

    //set the icon with the new size to the marker
    marker.setIcon({
        url: icon,
        scaledSize: new google.maps.Size(markerWidth, markerHeight)
    });
});

当然,您可以更改计算,使图标大小以不同的方式更改。我刚刚创建了一个快速示例。我刚刚更新了。

根据您的评论:是的,您可以使用标记解决它,但标记选项中没有“随时可用”选项。您可以添加缩放更改侦听器,并为标记图标设置新的比例。而不是地面覆盖层

var icon = 'https://www.google.com/mapfiles/marker_black.png';
var iconBounds = constructBounds(38.713107, -90.42984);
var staticOverlay = new google.maps.GroundOverlay(icon, iconBounds);
staticOverlay.setMap(map);
你可以用记号笔

var icon = 'https://www.google.com/mapfiles/marker_black.png';
var marker = new google.maps.Marker({
    position: {
        lat: 38.713107,
        lng: -90.42984
    },
    map: map,
    icon: icon
});
然后,您只需添加缩放更改侦听器,并根据缩放级别计算新的大小:

//add a zoom change listener, so you can resize the icon based on the zoom level
google.maps.event.addListener(map, 'zoom_changed', function() {

    var zoom = map.getZoom();
    markerWidth = (zoom/9)*20
    markerHeight = (zoom/9)*34

    //set the icon with the new size to the marker
    marker.setIcon({
        url: icon,
        scaledSize: new google.maps.Size(markerWidth, markerHeight)
    });
});

当然,您可以更改计算,使图标大小以不同的方式更改。我刚刚创建了一个快速示例。我刚刚更新。

上面有什么不“流畅”的地方?你对“平滑”的定义是什么?@MrUpsidown我认为可以在新的google.maps.Marker中作为一个选项。上面的“平滑”是什么?你对“平滑”的定义是什么?@MrUpsidown我认为可以在新的google.maps.Marker.The
MarkerImage
类中作为一个选项。很久以前(在v3.10之后)有人反对使用
图标
匿名对象。你是对的。我刚刚将弃用的
MarkerImage
类更新为
Icon
MarkerImage
类在很久以前(v3.10之后)被弃用,取而代之的是
Icon
匿名对象。你说得对。我刚刚将弃用的
标记图像
类更新为
图标