如何以固定像素距离获取两个 Google 地图标记

问题描述 投票:0回答:1

我一直在努力寻找问题的解决方案:我需要在地图中心周围放置两个标记,留出足够的空间,以便它们之间没有任何覆盖,无论缩放或地图位置如何(因为纬度不是与垂直位置不成线性)。我知道投影可以帮助我,但我不知道如何使用它们。我该怎么办?

typescript google-maps google-maps-markers coordinate-transformation map-projections
1个回答
0
投票

简而言之,这就是解决方案:

/** 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/

检查 有关投影的 Google 文档

© www.soinside.com 2019 - 2024. All rights reserved.