Rotate marker in Leaflet

后端 未结 4 2067
天命终不由人
天命终不由人 2021-02-04 06:59

How can I rotate a marker in leaflet? I will have a lot of markers, all with a rotation angle.

I\'ve tried this solution from runanet/coomsie at Leaflet on GitH

4条回答
  •  隐瞒了意图╮
    2021-02-04 07:40

    If you're using react-leaflet, I built upon this idea (https://github.com/bbecquet/Leaflet.RotatedMarker) to create a React component that extends Marker and accepts both rotation and rotationOrigin as a prop.

    // Libs
    import L from 'leaflet'
    
    // Components
    import { ExtendableMarker } from 'react-leaflet-extendable'
    
    // HOCS
    import { withLeaflet } from 'react-leaflet'
    
    const proto_setPos = L.Marker.prototype._setPos
    
    const LeafletMarker = L.Marker.extend({
      _setPos(pos: [number, number]) {
        proto_setPos.call(this, pos)
        this._setRotation(this.options.rotation)
      },
      _setRotation(rotation: number | null | undefined) {
        if (typeof rotation === 'number') {
          this._icon.style[L.DomUtil.TRANSFORM + 'Origin'] = this.options.rotationOrigin || 'center'
          const transform = this._icon.style[L.DomUtil.TRANSFORM] + ` rotate(${rotation}deg)`
          this._icon.style[L.DomUtil.TRANSFORM] = transform
        }
      },
    })
    
    const createRotatedMarker = (pos: [number, number], options: any) => {
      return new LeafletMarker(pos, options)
    }
    
    class RotatedMarker extends ExtendableMarker {
      public createLeafletElement() {
        return createRotatedMarker(this.props.position, { ...this.props })
      }
    }
    
    export default withLeaflet(RotatedMarker)

提交回复
热议问题