Encoding Latitude and Longitude pairs for performance - javascript encode

This Page is locked
Modified: 2007/06/13 11:00 by ViaVE Visitor
This function encodes an array of VELatLong into a string for simplicity and performance.


// Encode a signed number in the encode format.
function encodeSignedNumber(num) {
  var sgn_num = num << 1;

if (num < 0) { sgn_num = ~(sgn_num); }

return(encodeNumber(sgn_num)); }

// Encode an unsigned number in the encode format. function encodeNumber(num) { var encodeString = "";

while (num >= 0x20) { encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63)); num >>= 5; }

encodeString += (String.fromCharCode(num + 63)); return encodeString; }

// Create the encoded bounds. function createEncodings(points) { var i = 0; var plat = 0; var plng = 0; var encoded_points = "";

for(i = 0; i < points.length; ++i) { var point = points[i]; var lat = point.Latitude; var lng = point.Longitude;

var late5 = Math.floor(lat * 1e5); var lnge5 = Math.floor(lng * 1e5);

dlat = late5 - plat; dlng = lnge5 - plng;

plat = late5; plng = lnge5;

encoded_points += encodeSignedNumber(dlat) + encodeSignedNumber(dlng); } return encoded_points; }