rotate polygon around point in leaflet map

前端 未结 3 800
小鲜肉
小鲜肉 2020-12-18 15:50

I have an issue, in my leaflet map I\'ve created a triangle from polygon:

var polygon = L.polygon([ 
    [parseFloat(decimal_lat),parseFloat(decimal_lon)], 
         


        
3条回答
  •  北海茫月
    2020-12-18 16:14

    I adapted @dooderson and @MBo from this question answers, the final code is something like this, it works perfectly!

    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
    }
    

提交回复
热议问题