0

I have chosen this plugin because it seems like the best way to animate a marker on a map polyline.

It's not on npm so I just added module.exports = L.animatedMarker to AnimatedMarker.js from the repository and required it.

App.js

<AnimatedMarkerElement 
  route={this.state.route} 
  map={this.refs.map.leafletElement}
/>

AnimatedMarkerElement.js

require('./AnimatedMarker')
import { MapComponent } from 'react-leaflet'
import L from 'leaflet'

export default class AnimatedMarkerElement extends MapComponent {

  componentWillReceiveProps(nextProps) {
    const line = L.polyline([nextProps.route.coordinates]),
      animatedMarker = L.animatedMarker(line.getLatLngs())

    nextProps.map.addLayer(animatedMarker)
  }

  render() {
    return null
  }
}

The error is:

Uncaught TypeError: Cannot read property 'lat' of null

I think that the plugin is looking for a map that it can't find because its buried inside react elements.

7
  • Check if line.getLatLngs() returns anything
    – Alex Parij
    Commented Nov 28, 2016 at 14:34
  • That returns an array of latlng objects.
    – Jake Hm
    Commented Nov 28, 2016 at 14:41
  • Is the plugin compatible with your Leaflet version 0.7 or 1.0 ?
    – Alex Parij
    Commented Nov 28, 2016 at 14:43
  • Also why are you using nextProps.map ? You should not get map in your next props , try to use this.refs.map
    – Alex Parij
    Commented Nov 28, 2016 at 14:45
  • this.refs.map.leafletElement right? I pass that as a property to AnimatedMarkerElement.
    – Jake Hm
    Commented Nov 28, 2016 at 14:48

1 Answer 1

0
const line = L.polyline([nextProps.route.coordinates]),

should actually be

const line = L.polyline(nextProps.route.coordinates),

I was nesting an array inside an array.

Not the answer you're looking for? Browse other questions tagged or ask your own question.