android获取并解析Google Directions

时间:2010-06-03 10:17:26

标签: java android google-maps

google Directions 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> 

感谢

4 个答案:

答案 0 :(得分:21)

我在网上发现了这个例子我会尝试使用它。 polyline decoding example

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;
}

答案 1 :(得分:3)

我还试图在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);
    }
}

答案 2 :(得分:3)

我已经调整了urobo上面的答案(非常轻微),为你提供了谷歌地图for Android V2所需的LatLngs:

private List<LatLng> decodePoly(String encoded) {

    List<LatLng> poly = new ArrayList<LatLng>();
    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;

        LatLng p = new LatLng((double) lat / 1E5, (double) lng / 1E5);
        poly.add(p);
    }
    return poly;
}

答案 3 :(得分:0)

您可能希望快速查看将由waypoint_polyline和坐标列表直接创建的路径。为此谷歌已发布解码api“交互式折线编码器实用程序”

您可以将waypoint_polyline值粘贴到地址处的编码折线文本字段 Interactive Polyline Encoder Utility