我希望 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/