user3316068
user3316068

Reputation: 67

Animated marker with Mapbox GL JS

I would like to animate (having an animated gif, or a png sequence) a marker with mapbox gl js.

Does anyone has any link/doc/ressource talking about it? I can't find nothing but marker animation along lines.

Thanks in advance

Upvotes: 5

Views: 2070

Answers (1)

Dipen Shah
Dipen Shah

Reputation: 26095

Sure, you can use this example and create its variations based on your needs.

For example, use following code to display gif marker:

mapboxgl.accessToken = '<api key>';
var map = new mapboxgl.Map({
  container: 'map',
  style: 'mapbox://styles/mapbox/streets-v9',
  center: [-74.50, 40],
  zoom: 9
});

function addeMarkerToMap(map, coordinates) {
  // create a DOM element for the marker
  var el = document.createElement('div');
  el.className = 'marker';

  // add marker to map
  new mapboxgl.Marker(el)
    .setLngLat(coordinates)
    .addTo(map);
}

addeMarkerToMap(map, [-74.5, 40]);
body {
  margin: 0;
  padding: 0;
}

#map {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}

.marker {
  background-image: url(https://media.giphy.com/media/Bfa45K0r6cCIw/giphy.gif);
  width: 32px;
  height: 32px;
}
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.css' rel='stylesheet' />

<div id='map' class="myMap"></div>

PS: Replace <api key> with your actual API key to run the example code without authorization warning.

Upvotes: 4

Related Questions