解码 Google 导航服务 (DirectionsService) 返回的坐标串

Google 导航服务 (DirectionsService) 返回的坐标串是经过编码的,需要经过解码才能使用,下面的代码可以对 Google 导航服务 (DirectionsService) 返回的坐标串进行解码:

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

posted @ 2010-05-31 12:55  张志敏  阅读(791)  评论(2编辑  收藏  举报