android 获取并解析 Google Directions

2022-09-04 07:31:24

谷歌方向API

我现在阅读了本指南,我可以构建一个正确的请求来接收包含从地址A到地址B的方向的xml文件。我需要的是一些关于如何阅读此xml以在Android MapView上绘制获得的方向的说明和示例。我还想知道在xml中表示此标记的内容:

<overview_polyline>
<points>
a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\|tNtqf@l{Yd_Fblh@rxo@b}
@xxSfytAblk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@
xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@lo
Ppxq@bw_@v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp]
fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvH
x`k@dse@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_B
flmA~yQftNboWzoAlzp@mz`@|}_@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC
</points>
<levels>BBBAAAAABAABAAAAAABBAAABBAAAABBAAABABAAABABBAABAABAAAABABABABBABAABB</levels> 
</overview_polyline> 

谢谢


答案 1

我在网络上发现了这个例子,我会尝试使用它。折线解码示例

private List<GeoPoint> decodePoly(String encoded) {

  List<GeoPoint> poly = new ArrayList<GeoPoint>();
  int index = 0, len = encoded.length();
  int lat = 0, lng = 0;

  while (index < len) {
      int b, shift = 0, result = 0;
      do {
          b = encoded.charAt(index++) - 63;
          result |= (b & 0x1f) << shift;
          shift += 5;
      } while (b >= 0x20);
      int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
      lat += dlat;

      shift = 0;
      result = 0;
      do {
          b = encoded.charAt(index++) - 63;
          result |= (b & 0x1f) << shift;
          shift += 5;
      } while (b >= 0x20);
      int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
      lng += dlng;

      GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),
           (int) (((double) lng / 1E5) * 1E6));
      poly.add(p);
  }

  return poly;
}

答案 2

我还试图在Android中使用Google的Direction Api。所以我做了一个开源项目来帮助做到这一点。你可以在这里找到它:https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

它是如何工作的,肯定很简单:

public class MainActivity extends ActionBarActivity implements DCACallBack{
/**
 * Get the Google Direction between mDevice location and the touched location using the     Walk
 * @param point
 */
private void getDirections(LatLng point) {
     GDirectionsApiUtils.getDirection(this, startPoint, endPoint, GDirectionsApiUtils.MODE_WALKING);
}

/*
 * The callback
 * When the directions is built from the google server and parsed, this method is called and give you the expected direction
 */
@Override
public void onDirectionLoaded(List<GDirection> directions) {        
    // Display the direction or use the DirectionsApiUtils
    for(GDirection direction:directions) {
        Log.e("MainActivity", "onDirectionLoaded : Draw GDirections Called with path " + directions);
        GDirectionsApiUtils.drawGDirection(direction, mMap);
    }
}

推荐