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 us at apisupport@mappls.com
Support
Need support? contact us!
@ Copyright 2022 CE Info Systems Ltd. All Rights Reserved.
Terms & Conditions | Privacy Policy | Supplier Sustainability Policy | Health & Safety Policy | Environmental Policy & CSR Report
Customer Care: +91-9999333223