如何檢查自定義工具提示的左上角是否適合地圖邊界? 我正在使用下面的代碼,但有些東西不對,因爲'myLatLng'永遠不會在地圖範圍內。提前謝謝你的幫助。檢查自定義工具提示的左上角是否在邊界內
var scale = Math.pow(2, map.getZoom());
var nw = new google.maps.LatLng(
map.getBounds().getNorthEast().lat(),
map.getBounds().getSouthWest().lng()
);
var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw);
var worldCoordinate = map.getProjection().fromLatLngToPoint(marker.getPosition());
var pixelOffset = new google.maps.Point(
Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale),
Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale)
);
var myLatLng = map.getProjection().fromPointToLatLng(pixelOffset);
if (map.getBounds().contains(myLatLng)) {
// it's within bounds
}
當然,「標記錨點的位置」是標記的位置,即'marker.getPosition()'? –
我需要計算標記定位點的像素位置 – Chris
爲什麼要將其轉換爲px座標,然後返回latLng來計算標記是否在邊界內? '.contains()'方法最終會直接在標記位置上運行; 'if(map.getBounds()。contains(marker.getPosition())){/ *它在範圍內* /}' –