Skip to content

Decoding Geometry JavaScript

Kunal Bharti edited this page May 21, 2019 · 1 revision

MapmyIndia APIs

Decoding Geometry in JavaScript

Decode Geometry to get route array:

function decode(encoded) {
    var points = [],index = 0, len = encoded.length;
    var lat = 0, lng = 0;
    while (index < len) {
        var b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++).charCodeAt(0) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
       var dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;shift = 0;result = 0;
        do {
            b = encoded.charAt(index++).charCodeAt(0) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        var dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;points.push([lat / 1E5, lng / 1E5])
    }
    return points
}

Example

The above returns an array of way-points for the route; such as:

 [[28.61415, 77.21218],[28.61468, 77.21221],[28.61526, 77.21225],[28.61548, 77.21226],[28.61592, 77.21229],[28.61631, 77.21232],[28.61662, 77.21234],[28.61664, 77.21223]]

For any queries and support, please contact:

Email Email us at apisupport@mapmyindia.com

Stack Overflow Ask a question under the mapmyindia-api

Support Need support? contact us!

Blog Read about the latest updates & customer stories

© Copyright 2019. CE Info Systems Pvt. Ltd. All Rights Reserved. | Terms & Conditions Written with StackEdit by MapmyIndia.

Clone this wiki locally