2013-03-10 103 views
2

中的http请求我知道这个问题有很多话题,但我的问题更具体。 我打电话给谷歌方向http API来获取两个位置之间的路由。响应的JSON包含了如何从A点到B点的所有信息。但是我如何计算我自己的距离,例如我在实际位置和下一步移动的结束位置之间的距离?我需要的是“道路线”而不是“航空线”。获取两个位置之间的距离,如Google路线

我所做的是使用LocationManager获取我的实际位置的lat,lng。有了这个和lat,结束位置的lng我称之为Location.distanceBetween方法。但它提供了一个不可思议的距离..不精确意味着方法之间的距离的结果不同于方向API之外的距离。对于很短的方式,很长的路要走。

一个解决方案是多次调用Directions API ...但是必须有更好的工作来获得沿道路的实际距离。有没有一种工具来获得我想要的?

编辑,以使其更容易理解

回答

1

请对方向API的调用:

public Document getDocument(LatLng start, LatLng end, String mode) { 
    String url = "http://maps.googleapis.com/maps/api/directions/xml?" 
      + "origin=" + start.latitude + "," + start.longitude 
      + "&destination=" + end.latitude + "," + end.longitude 
      + "&sensor=false&units=metric&mode=driving"; 

    try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse response = httpClient.execute(httpPost, localContext); 
     InputStream in = response.getEntity().getContent(); 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = builder.parse(in); 
     return doc; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

之后,你可以在所有的JSON运行对象,你在文件中已经收到,并检查distnace每对点之间:

public int getDistanceValue (Document doc) { 
    NodeList nl1 = doc.getElementsByTagName("distance"); 
    Node node1 = nl1.item(0); 
    NodeList nl2 = node1.getChildNodes(); 
    Node node2 = nl2.item(getNodeIndex(nl2, "value")); 
    Log.i("DistanceValue", node2.getTextContent()); 
    return Integer.parseInt(node2.getTextContent()); 
} 

,这里是你如何让所有的点:

public ArrayList<LatLng> getDirection (Document doc) { 
    NodeList nl1, nl2, nl3; 
    ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>(); 
    nl1 = doc.getElementsByTagName("step"); 
    if (nl1.getLength() > 0) { 
     for (int i = 0; i < nl1.getLength(); i++) { 
      Node node1 = nl1.item(i); 
      nl2 = node1.getChildNodes(); 

      Node locationNode = nl2.item(getNodeIndex(nl2, "start_location")); 
      nl3 = locationNode.getChildNodes(); 
      Node latNode = nl3.item(getNodeIndex(nl3, "lat")); 
      double lat = Double.parseDouble(latNode.getTextContent()); 
      Node lngNode = nl3.item(getNodeIndex(nl3, "lng")); 
      double lng = Double.parseDouble(lngNode.getTextContent()); 
      listGeopoints.add(new LatLng(lat, lng)); 

      locationNode = nl2.item(getNodeIndex(nl2, "polyline")); 
      nl3 = locationNode.getChildNodes(); 
      latNode = nl3.item(getNodeIndex(nl3, "points")); 
      ArrayList<LatLng> arr = decodePoly(latNode.getTextContent()); 
      for(int j = 0 ; j < arr.size() ; j++) { 
       listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude)); 
      } 

      locationNode = nl2.item(getNodeIndex(nl2, "end_location")); 
      nl3 = locationNode.getChildNodes(); 
      latNode = nl3.item(getNodeIndex(nl3, "lat")); 
      lat = Double.parseDouble(latNode.getTextContent()); 
      lngNode = nl3.item(getNodeIndex(nl3, "lng")); 
      lng = Double.parseDouble(lngNode.getTextContent()); 
      listGeopoints.add(new LatLng(lat, lng)); 
     } 
    } 

    return listGeopoints; 
} 

其实我用这个代码在地图上绘制了一个方向的Polyline,但距离函数也应该可以工作。

+0

您的代码描述了如何从路线API响应中获取路线信息。但我需要从移动时的实际位置生成距离API的step/polyline距离值。 – 2013-03-10 19:51:54

相关问题