2013-03-10 102 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

相關問題