首先要提到的是,我曾见过类似How to offset the center point in Google maps api V3的帖子,但并没有完成我想要实现的目标。
我的情况如下:我有一个Google Map,要在其中设置倾斜度,用户可以缩放/缩放,我想稍微向上移动地图中心,以使用户的位置标记成为多下来。目前,我正在使用以下代码进行操作:
private async centerMapWithOffset(latlng: ILatLng): Promise<void> {
return new Promise(async (resolve, reject) => {
this.map.setCameraZoom(100);
let point1 = await this.map.fromLatLngToPoint(latlng);
let offsety = (window.screen.availHeight * 0.3 * -1);
this.map.animateCamera({
target: await this.map.fromPointToLatLng([point1[0], point1[1] + offsety]),
duration: 1000
});
resolve();
});
}
当前有效。但是我现在有两个问题:
this.map.setCameraZoom(100)
开始y偏移量计算之前,必须将缩放比例设置为100%。因为如果用户使用的缩放比例不是100%,它将无法正确地使地图居中,而将位置标记留在屏幕底部附近。tilt
。但是同样,如果在进行微积分之前或之后设置了倾斜度,它将无法正确地使我的地图居中。在我一开始引用的帖子中,他们使用Math.pow(2, map.getZoom())
,但是如果使用它,它将使地图完全居中,而不是按照我的意愿向上移动。至于tilt
,没有任何引用...
我理想的代码应该是这样的,但是我却错过了我必须做的演算...:
private async centerMapWithOffset(latlng: ILatLng): Promise<void> {
return new Promise(async (resolve, reject) => {
let point1 = await this.map.fromLatLngToPoint(latlng);
let offsety = calculus...;
this.map.animateCamera({
target: await this.map.fromPointToLatLng([point1[0], point1[1] + offsety]),
duration: 1000,
tilt: 50,
zoom: 100
});
resolve();
});
}
This is the plugin I am using.
我希望你们能帮上忙,谢谢。