Loading

Moving Car Animation Using Mapbox

Moving Car Animation Using Mapbox

Day by day mobile application is more demanding, attracting most to the customer as well as developers. Tracker application or any kind of live tracking system made transportation and other export/import business easier. Everyone wants to track their vehicle whether it is a company or a driver or a user who is using it. They can get all the information at their fingertips.

Nowadays, developers have many options to build this type of application very easily using Google Maps, Mapbox or Leaflet, etc. Other than the official website, there are many articles out there over the internet to guide how to display a map with a custom car marker, draw a route between two points. Nevertheless, sometimes developers might get in trouble while developing in some cases.

In this article, I'm going to share how can we integrate moving car animation between points in our mobile or web application using Mapbox. Because very less article is out there regarding this. Using Mapbox API developers can easily locate current vehicle locations, draw the routes vehicle has traveled, or going to travel. But can't find out any article of a smooth car moving animation over the internet. So, my main concern here is to help the developers to achieve that using Mapbox.

The first step is to create an HTML div element, assign a class name (where the car marker icon is imported) into it, create an instance of mapboxgl marker and pass the div element while creating. Check the sample below:

var el = document.createElement('div');
el.className = 'carmarker';
var marker = new mapboxgl.Marker(el);

Our next step is to set current latitidue-longitude and rotation to the marker instance and add to the map. Check it below:

marker.setLngLat([current_lng, current_lat]);
.setRotation(current_rotation_or_heading)
marker.addTo(map);

The next step is to move the car icon smoothly from the current position to a new position. To do that we need to get the new latitude-longitude and new rotation of the car. And if we follow the same step as above we will get the rapid car moving that is a challenge. To fix this issue, we have created the following algorithm. The algorithm will calculate the difference between the new position and current position, then divide the result by the conditional delta.

var num_deltas = 200;
var steps = 0;
var position = [marker.getLngLat().lng, marker.getLngLat().lat];
var lng = NewLocation_lng - marker.getLngLat().lng;
var lat = NewLocation_lat - marker.getLngLat().lat;
var delta_lng = lng / num_deltas;
var delta_lat = lat / num_deltas;

Now we would like to give direction to our marker towards it is moving. To make the direction we use the turf library which helps to determine the angle between two positions. To find out the angle, we transform our coordinates into points using the function turf.point(), then get the angle between these two points using the function turf.rhumbBearing().

angle = turf.rhumbBearing(turf.point(NewLocation), turf.point([marker.getLngLat().lng, marker.getLngLat().lat])) + 180;

Do not include the full library. The two modules can be enough @turf/rhumb-bearing and @turf/helpers.

Now we have the angle it is moving and the new position. Keep updating the new coordinates and angle (rotation or heading) to the map as above.

function driveCar() => {
    position[0] += deltaLng;
    position[1] += deltaLat;

    // update marker location using position which the new car position 
    // and car direction using angle 

    if (steps !== numDeltas) {
        steps++;
        setTimeout(() => driveCar(), 100);
    }
}

Thus we will get a smooth car moving animation.

Related Articles