我正在尝试使用此 question 中的代码旋转 Leaflet 矩形。
rotatePoints (center, points, yaw) {
const res = []
const angle = yaw * (Math.PI / 180)
for (let i = 0; i < points.length; i++) {
const p = points[i]
// translate to center
const p2 = new LatLng(p.lat - center.lat, p.lng - center.lng)
// rotate using matrix rotation
const p3 = new LatLng(Math.cos(angle) * p2.lat - Math.sin(angle) * p2.lng, Math.sin(angle) * p2.lat + Math.cos(angle) * p2.lng)
// translate back to center
const p4 = new LatLng(p3.lat + center.lat, p3.lng + center.lng)
// done with that point
res.push(p4)
}
return res
}
问题是矩形在旋转时倾斜。 有什么想法如何优化这个功能吗?
已修复 最终代码:
rotatePoints (center, points, yaw) {
const res = []
const centerPoint = map.latLngToLayerPoint(center)
const angle = yaw * (Math.PI / 180)
for (let i = 0; i < points.length; i++) {
const p = map.latLngToLayerPoint(points[i])
// translate to center
const p2 = new Point(p.x - centerPoint.x, p.y - centerPoint.y)
// rotate using matrix rotation
const p3 = new Point(Math.cos(angle) * p2.x - Math.sin(angle) * p2.y, Math.sin(angle) * p2.x + Math.cos(angle) * p2.y)
// translate back to center
let p4 = new Point(p3.x + centerPoint.x, p3.y + centerPoint.y)
// done with that point
p4 = map.layerPointToLatLng(p4)
res.push(p4)
}
return res
}
最佳答案
球体上的“矩形”是什么?这是将当前投影应用到这样的坐标的结果,使得它们在 map /屏幕上的图像形成矩形。请注意,每个矩形边的经纬度坐标不应相等(例如,在大多数常用投影中,与赤道对齐的矩形的顶部和底部点的经度将有所不同)
所以为了在 map 上得到好看的矩形,你需要在屏幕坐标中旋转顶点,并在经纬空间中进行反投影。
关于javascript - 旋转传单折线/矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60615163/