解碼 Google 導(dǎo)航服務(wù) (DirectionsService) 返回的坐標(biāo)串
Google 導(dǎo)航服務(wù) (DirectionsService) 返回的坐標(biāo)串是經(jīng)過(guò)編碼的,需要經(jīng)過(guò)解碼才能使用,下面的代碼可以對(duì) Google 導(dǎo)航服務(wù) (DirectionsService) 返回的坐標(biāo)串進(jìn)行解碼:
function decodePoints(Vb) {
var Ih = Vb.length;
var pb = 0;
var ba = new Array();
var Ka = 0;
var Pa = 0;
while (pb < Ih) {
var ub;
var oc=0;
var Fa=0;
do {
ub = Vb.charCodeAt(pb++) - 63;
Fa |= (ub & 31) << oc;
oc += 5
}
while (ub >= 32);
Ka = Ka + (Fa & 1 ? ~(Fa>>1) : Fa>>1);
ba.push(Ka);
oc = 0;
Fa = 0;
do {
ub = Vb.charCodeAt(pb++) - 63;
Fa |= (ub&31) << oc;
oc += 5
}
while (ub >= 32);
Pa = Pa + (Fa & 1 ? ~(Fa>>1) : Fa>>1);
ba.push(Pa)
}
for (var i = 0; i < ba.length; i++) {
ba[i] *= 1.0E-5;
}
//return { points: ba, numPoints: ba.length >> 1 };
var p = [];
for (var i = 0; i < ba.length; i+=2) {
p.push({ lat: ba[i], lng: ba[i+1]});
}
return p;
}
這個(gè)算法是從 Google Maps v2 中找到的, 現(xiàn)在還可以使用, 希望 Google 不會(huì)那么快就修改編碼的算法。
參考資料:
http://libgmail.sourceforge.net/googlemaps/maps.2.html
http://libgmail.sourceforge.net/googlemaps/maps.3.html
張志敏所有文章遵循創(chuàng)作共用版權(quán)協(xié)議,要求署名、非商業(yè) 、保持一致。在滿(mǎn)足創(chuàng)作共用版權(quán)協(xié)議的基礎(chǔ)上可以轉(zhuǎn)載,但請(qǐng)以超鏈接形式注明出處。
本博客已經(jīng)遷移到 GitHub , 圍觀地址: https://beginor.github.io/
浙公網(wǎng)安備 33010602011771號(hào)