javascript - 防止标记在缩小时缩放

标签 javascript google-maps-api-3 google-maps-markers

我希望 Google map 标记在特定缩放级别上具有固定大小,如本例所示:http://jsfiddle.net/bryan_weaver/4rxqQ/

function initialize() {
var map;
var centerPosition = new google.maps.LatLng(38.713107, -90.42984);
var options = {
    zoom: 9,
    center: centerPosition,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map($('#map')[0], options);

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);
}

function constructBounds(lat, lng){
var sw = new google.maps.LatLng(lat - .03, lng - .025)
var ne = new google.maps.LatLng(lat + .03, lng + .025)
return new google.maps.LatLngBounds(sw, ne);
}

google.maps.event.addDomListener(window, 'load', initialize);

但是还有比上面 Jsfiddle 更顺利的解决方案吗?

最佳答案

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

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)
    });
});

当然,您可以更改计算方式,以便图标大小以不同的方式发生变化。我刚刚创建了一个简单的例子。我刚刚更新your fiddle .

关于javascript - 防止标记在缩小时缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41648702/

相关文章:

javascript - 使用外部按钮控制缩放级别

google-maps-api-3 - 谷歌地图地理编码 api 不一致

google-maps - Google Maps自定义标签的x和y位置

javascript - Angular $watch 功能不适用于模型更改

javascript - nodejs递归调用相同的api并顺序写入excel文件

javascript - 这个跨度如何在选择框中不可见?

javascript - 在 Gmaps API 上循环标记时 setTimeout 函数不起作用

android - 将图像放入 Mapview 标记标注

css - 当我尝试将 Google map 集成到现有网页时,标记没有出现

javascript - 将单击 javascript 事件应用于 JSP 中的 header 而不是使用 <a> 标记