8
A
回答
6
您應該可以根據the docs在縮放更改上添加偵聽器。它不會傳遞任何東西,但你可以通過api獲取屬性。
google.maps.event.addListener(map, 'zoom_changed', function() {
zoomLevel = map.getZoom();
//this is where you will do your icon height and width change.
});
8
這是我最終使用的代碼:
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;
});
3
此代碼將改變每個縮放級別變化,使圖標顯示爲相同的地理大小時圖標的大小。
//create a marker image with the path to your graphic and the size of your graphic
var markerImage = new google.maps.MarkerImage(
'myIcon.png',
new google.maps.Size(8,8), //size
null, //origin
null, //anchor
new google.maps.Size(8,8) //scale
);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(38, -98),
map: map,
icon: markerImage //set the markers icon to the MarkerImage
});
//when the map zoom changes, resize the icon based on the zoom level so the marker covers the same geographic area
google.maps.event.addListener(map, 'zoom_changed', function() {
var pixelSizeAtZoom0 = 8; //the size of the icon at zoom level 0
var maxPixelSize = 350; //restricts the maximum size of the icon, otherwise the browser will choke at higher zoom levels trying to scale an image to millions of pixels
var zoom = map.getZoom();
var relativePixelSize = Math.round(pixelSizeAtZoom0*Math.pow(2,zoom)); // use 2 to the power of current zoom to calculate relative pixel size. Base of exponent is 2 because relative size should double every time you zoom in
if(relativePixelSize > maxPixelSize) //restrict the maximum size of the icon
relativePixelSize = maxPixelSize;
//change the size of the icon
marker.setIcon(
new google.maps.MarkerImage(
marker.getIcon().url, //marker's same icon graphic
null,//size
null,//origin
null, //anchor
new google.maps.Size(relativePixelSize, relativePixelSize) //changes the scale
)
);
});
相關問題
- 1. 根據縮放級別縮放自定義標記Google地圖
- 2. Leaflet ::根據縮放級別改變圖標
- 3. 根據縮放級別更改列數:HighChart(列圖類型)
- 4. 有沒有辦法根據縮放級別更改圖標圖像? (leaflet.js)
- 5. 更改地圖中的縮放級別
- 6. 更改高地圖的縮放級別
- 7. Google Maps V3:根據地圖縮放級別縮放折線?
- 8. 根據縮放級別渲染QGraphicsScene
- 9. Google-Maps v3:如何根據縮放級別更改地圖樣式?
- 10. Mapbox for Android:根據當前縮放級別更改地圖輸入
- 11. 更改子窗口的縮放級別
- 12. 更改顯示縮放級別
- 13. Openlayers 3:當縮放級別改變時更改向量圖層
- 14. 根據Google Maps API的縮放級別更新邊界 - 修訂
- 15. 如何根據縮放級別加載圖層?
- 16. 根據縮放級別顯示WMS圖層
- 17. 根據縮放級別限制地圖疊加層的數量
- 18. 根據顯示分辨率設置Google地圖縮放級別
- 19. 更改alpha級別的javascript縮略圖
- 20. 如何使標記根據縮放級別調整其大小Google地圖v2
- 21. 如何根據標記位置設置Google地圖的縮放級別
- 22. 更改Fusiontable的地圖可視化縮放級別
- 23. 更改Google地圖在Jquery中的縮放級別
- 24. 更改默認縮放級別2地圖
- 25. 圖標根據縮放因子
- 26. 根據Silverlight的Bing地圖控件中的縮放級別更改圖釘可見性
- 27. MkMapView縮放級別
- 28. UIWebView縮放級別
- 29. 檢查縮放級別是否改變
- 30. 按縮放級別改變半徑
莫名其妙地處理一些onzoom事件,然後只是改變圖標?不知道,不要與v3一起工作... – TMS