Mapbox Matching API switched coordinates

650 Views Asked by At

Edit: I reformatted the question as it was pointed out to me that the problem is not in the Mapbox API.

As of a few days ago an application using mapbox matching API coupled with leaflet.js started drawing polylines on the other side of the planet.

While the path it self looks as it should be, leaflet drew it on another continent. The Mapbox Matching API returns the response following GEOJson standard as:

{
    "code": "Ok",
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": "e`s`YmyazuAg`@y]uo@ej@eQcOkFkEyCeCaYmUuFoMkWgOo[_ReGqDoGyDmVaPaU|RoDlDsa@z`@sTdSXJf@j@??Jr@Mp@ML[Z}@ZiAHgAK]MaJzJsUbWkSxRyHpHuLhLuFzFqDrDq[`\\oPrPyK|KmU|TuLzKyClCr@ZZf@Fj@Ij@e@b@zn@fZ}Zf]gCdKmHxQErC[jV",
        "properties": {
            "confidence": 0.45413768894813844,
            "distance": 1366.4,
            "duration": 243.3,
            "matchedPoints": [
                [13.658131, 45.532583],
                [13.659851, 45.534127],
                [13.661445, 45.535438],
                [13.662397, 45.535398],
                [13.663582, 45.534237],
                [13.666378, 45.531441],
                [13.666457, 45.529215]
            ],
            "indices": [0, 1, 2, 3, 4, 5, 6]
        }
    }]
}

Using the code below to draw the layer with leaflet.js results in a polyline drawn on a wrong position.

L.mapbox.mapmatching(geojson, options, function (error, layer) {
            layer.addTo(map);
            layer.setStyle({
                color: '#3c8dbc',
                weight: 4,
                opacity: 0.8
            });
            //fit bounds to added layer
            map.fitBounds(layer.getBounds());

The result being:

Polyline drawn in Yamen instead of Europe

While the polyline it self is as it should be, the position is not. I speculate the problem is that leaflet expect the coordinates to be in format [latitude, longitude] while the Mapbox matching API returns GEOJson format namely [longitude, latitude].

Should I manually switch the coordinates of the response before drawing it or am I just doing it wrong?

Thanks.

Edit 2 : It appears the problem is in fact in different formats. More about it can be found in this post Changing the order of the coordinates in the returned object is very tedious hence an elegant solution to this would be greatly appropriated.

0

There are 0 best solutions below