Google 导航服务 (DirectionsService) 返回的坐标串是经过编码的,需要经过解码才能使用,下面的代码可以对 Google 导航服务 (DirectionsService) 返回的坐标串进行解码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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;
}
|
这个算法是从 Google Maps v2 中找到的, 现在还可以使用, 希望 Google 不会那么快就修改编码的算法。
参考资料:
http://libgmail.sourceforge.net/googlemaps/maps.2.html
http://libgmail.sourceforge.net/googlemaps/maps.3.html