JSFiddle - React, Tailwind, and code Playground
by shitao1988
JavaScript
/**
* APIFunction: decodeGooglePolyline
* This function will decode a google-style polyline and return an array
* of <OpenLayers.Geometry.Point>
*
* Algorithm by http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
*
* Parameters:
* polyline - {String} encoded polyline
*
* Returns:
* {Array(<OpenLayers.LonLat>} decoded points array
*/
decodeGooglePolyline = function(polyline) {
var points = new Array();
var b;
var i = 0;
var lat = 0;
var lon = 0;
var result;
var shift;
while (i < polyline.length) {
result = 0;
shift = 0;
do {
b = polyline.charCodeAt(i++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
lat += ((result & 1) ? ~(result >> 1) : (result >> 1));
result = 0;
shift = 0;
do {
b = polyline.charCodeAt(i++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
lon += ((result & 1) ? ~(result >> 1) : (result >> 1));
points.push(new OpenLayers.LonLat(lon * 1e-5, lat * 1e-5));
}
return points;
};
/**
* APIFunction: decodeGoogleLoD
* This function will decode a google-style level of details string an array
*
* Algorithm by http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
*
* Parameters:
* levels - {String} encoded levels
* num_levels - {int} number of levels
*
* Returns:
* {Array(int} decoded levels array
*/
decodeGoogleLoD = function(levels, num_levels) {
var lod = new Array();
var b;
var i = 0;
var result;
var shift;
while (i < levels.length) {
result = 0;
shift = 0;
do {
b = levels.charCodeAt(i++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
lod.push(num_levels - result - 1);
}
return lod;
};
/**
* APIFunction: decodeGoogle
* This function will decode a google-style string to an array
*
* Algorithm...