How to parse Google Maps direction api's “overview_polyline”
How to parse Google Maps direction api's “overview_polyline”
1.In Javascript I use this function to decode Polylines in my Node.JS program. – josebetomex
// These functions decode a polyline pointstring.
// The first is mine and simply links the second function to the form.
function decode () {
var instring;
var outstring;
var points;
instring = document.getElementById("polylineDecoder").encodedPolylineIn.value;
instring = instring.replace(/\\\\/g, "\\");
points = decodeLine(instring);
outstring = "";
for(i=0; i < points.length; i++) {
outstring = outstring + points[i][0] + ", " + points[i][1] + "\n";
}
document.getElementById("polylineDecoder").decodedPolylineOut.value = outstring;
}
// This function is from Google's polyline utility.
function decodeLine (encoded) {
var len = encoded.length;
var index = 0;
var array = [];
var lat = 0;
var lng = 0;
while (index < len) {
var b;
var shift = 0;
var result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
array.push([lat * 1e-5, lng * 1e-5]);
}
return array;
}
//a}oeFfljjViEh@pAnS|AxU?v@UfBAPKVOd@GR?b@{@TEFCLIj@WrCSxCUhDAnA?VENOd@Kt@EvAClCDlAEp@Oj@S^ELAROlAGXI\Ob@e@hCeAxGU`DCdA@~@HzED`EBt@Jn@VbAz@`B|@tA~@vAt@rA\v@X~@Pt@Df@Ab@Et@In@qArFOh@gAbEId@Cz@F`CFx@b@rD\dBd@tAn@xBNf@FTl@vBj@dCl@tDHz@Vn@Tj@Ll@Xt@B|@Hz@PZJNR\H^Dh@@d@BXHVH^Dj@?rBJp@@RAfABh@b@dCX`CLdA^zB`@~Cd@zB?RQ`@X`@lAvFh@tCZlAPz@b@pBFr@@~@EdAQx@OZd@rCn@vCnApEj@`BTbAL~@R|A@pBEnBEz@GXeAxDaAbASJJZHTHF`AEZ@HHd@bAPh@BROtAKd@Bb@J|@HHhAz@RtBPj@b@lAL|@XbALfAFXb@hBCpAD`@Nz@?x@BvAHj@Ct@FZB`BQ@SAo@K{@IuB?sEHwDTw@H@RJX@~@qCR
//ht tp:/ / s tackoverflow.com/questions/6082440/how-to-parse-google-maps-direction-apis-overview-polyline
2.if you got this encoded string on server side. here is decoder in C# – Saboor Awan
private List<Location> DecodePolylinePoints(string encodedPoints)
{
if (encodedPoints == null || encodedPoints == "") return null;
List<Location> poly = new List<Location>();
char[] polylinechars = encodedPoints.ToCharArray();
int index = 0;
int currentLat = 0;
int currentLng = 0;
int next5bits;
int sum;
int shifter;
try
{
while (index < polylinechars.Length)
{
// calculate next latitude sum = 0;
shifter = 0;
do
{
next5bits = (int)polylinechars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylinechars.Length);
if (index >= polylinechars.Length)
break;
currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
//calculate next longitude sum = 0;
shifter = 0;
do
{
next5bits = (int)polylinechars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylinechars.Length);
if (index >= polylinechars.Length && next5bits >= 32)
break;
currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
Location p = new Location();
p.Latitude = Convert.ToDouble(currentLat) / 100000.0;
p.Longitude = Convert.ToDouble(currentLng) / 100000.0;
poly.Add(p);
}
}
catch (Exception ex)
{
// logo it }
return poly;
}
<img id="img1" src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////
wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4ML
wWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="
alt="Base64 encoded image" width="50" height="50"/>
1.In Javascript I use this function to decode Polylines in my Node.JS program. – josebetomex
// These functions decode a polyline pointstring.
// The first is mine and simply links the second function to the form.
function decode () {
var instring;
var outstring;
var points;
instring = document.getElementById("polylineDecoder").encodedPolylineIn.value;
instring = instring.replace(/\\\\/g, "\\");
points = decodeLine(instring);
outstring = "";
for(i=0; i < points.length; i++) {
outstring = outstring + points[i][0] + ", " + points[i][1] + "\n";
}
document.getElementById("polylineDecoder").decodedPolylineOut.value = outstring;
}
// This function is from Google's polyline utility.
function decodeLine (encoded) {
var len = encoded.length;
var index = 0;
var array = [];
var lat = 0;
var lng = 0;
while (index < len) {
var b;
var shift = 0;
var result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
array.push([lat * 1e-5, lng * 1e-5]);
}
return array;
}
//a}oeFfljjViEh@pAnS|AxU?v@UfBAPKVOd@GR?b@{@TEFCLIj@WrCSxCUhDAnA?VENOd@Kt@EvAClCDlAEp@Oj@S^ELAROlAGXI\Ob@e@hCeAxGU`DCdA@~@HzED`EBt@Jn@VbAz@`B|@tA~@vAt@rA\v@X~@Pt@Df@Ab@Et@In@qArFOh@gAbEId@Cz@F`CFx@b@rD\dBd@tAn@xBNf@FTl@vBj@dCl@tDHz@Vn@Tj@Ll@Xt@B|@Hz@PZJNR\H^Dh@@d@BXHVH^Dj@?rBJp@@RAfABh@b@dCX`CLdA^zB`@~Cd@zB?RQ`@X`@lAvFh@tCZlAPz@b@pBFr@@~@EdAQx@OZd@rCn@vCnApEj@`BTbAL~@R|A@pBEnBEz@GXeAxDaAbASJJZHTHF`AEZ@HHd@bAPh@BROtAKd@Bb@J|@HHhAz@RtBPj@b@lAL|@XbALfAFXb@hBCpAD`@Nz@?x@BvAHj@Ct@FZB`BQ@SAo@K{@IuB?sEHwDTw@H@RJX@~@qCR
//ht tp:/ / s tackoverflow.com/questions/6082440/how-to-parse-google-maps-direction-apis-overview-polyline
2.if you got this encoded string on server side. here is decoder in C# – Saboor Awan
private List<Location> DecodePolylinePoints(string encodedPoints)
{
if (encodedPoints == null || encodedPoints == "") return null;
List<Location> poly = new List<Location>();
char[] polylinechars = encodedPoints.ToCharArray();
int index = 0;
int currentLat = 0;
int currentLng = 0;
int next5bits;
int sum;
int shifter;
try
{
while (index < polylinechars.Length)
{
// calculate next latitude sum = 0;
shifter = 0;
do
{
next5bits = (int)polylinechars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylinechars.Length);
if (index >= polylinechars.Length)
break;
currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
//calculate next longitude sum = 0;
shifter = 0;
do
{
next5bits = (int)polylinechars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylinechars.Length);
if (index >= polylinechars.Length && next5bits >= 32)
break;
currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
Location p = new Location();
p.Latitude = Convert.ToDouble(currentLat) / 100000.0;
p.Longitude = Convert.ToDouble(currentLng) / 100000.0;
poly.Add(p);
}
}
catch (Exception ex)
{
// logo it }
return poly;
}
<img id="img1" src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////
wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4ML
wWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="
alt="Base64 encoded image" width="50" height="50"/>
评论
发表评论