我花了几个小时寻找问题的解决方案:我需要在地图中心周围放置两个标记,留出足够的空间,使它们之间没有任何覆盖,无论缩放或地图位置如何(因为纬度不是与垂直位置不成线性)。
简而言之,这就是解决方案:
/** Returns the LatLng position at a pixel {@link distance} from {@link position} at the current zoom.
* @param {google.maps.LatLng} position the position to start from
* @param {{ x: number; y: number; }} distance the distance coordinates from {@link position} to calculate the location in. {@link distance}.x increases going to the right, {@link distance}.y increases going down.
*/
const getFixedPixelDistanceLocationFrom = (position: google.maps.LatLng, distance: { x: number; y: number; }) => {
const zoomScale = Math.pow(2, this.map!.getZoom()!),
projection = this.map!.getProjection()!,
centerPixel = projection.fromLatLngToPoint(position)!;
// The distance is intended at current zoom, hence the projection must be magnified accordingly
return projection.fromPointToLatLng(
new google.maps.Point(
((centerPixel.x * zoomScale) + distance.x) / zoomScale,
((centerPixel.y * zoomScale) + distance.y) / zoomScale
)
)!;
};
此方法允许您从给定的点获取固定像素距离的 LatLng 点。 演示:https://jsfiddle.net/massic80/6nsq7yo3/