1

我想獲取兩個geopoint座標之間的道路距離。目前我正在尋找路徑,但它不是很準確,因爲它考慮道路的起點和終點,但不考慮道路曲線。我正在使用下面的代碼:如何獲得最短的駕駛路徑和兩個地理座標之間的距離?

Double sLat = Double.parseDouble(getIntent().getStringExtra("sLat")); 
Double sLon = Double.parseDouble(getIntent().getStringExtra("sLon")); 
Double dLat = Double.parseDouble(getIntent().getStringExtra("dLat")); 
Double dLon = Double.parseDouble(getIntent().getStringExtra("dLon")); 

MapView mv = (MapView) findViewById(R.id.mapview); 
mv.setBuiltInZoomControls(true); 
MapController mc = mv.getController(); 
ArrayList<GeoPoint> all_geo_points = getDirections(sLat, sLon, dLat, dLon); 
GeoPoint moveTo = (GeoPoint) all_geo_points.get(0); 
mc.animateTo(moveTo); 
mc.setZoom(18); 
mv.getOverlays().add(new MyOverlay(all_geo_points)); 

    public static ArrayList<GeoPoint> getDirections(double lat1, double lon1, double lat2, double lon2) 
{ 
    String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" +lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric"; 
    String tag[] = { "lat", "lng" }; 
    ArrayList<GeoPoint> list_of_geopoints = new ArrayList<GeoPoint>(); 
    HttpResponse response = null; 
    try 
    { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpPost httpPost = new HttpPost(url); 
     response = httpClient.execute(httpPost, localContext); 
     InputStream in = response.getEntity().getContent(); 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = builder.parse(in); 
     if (doc != null) { 
      NodeList nl1, nl2; 
      nl1 = doc.getElementsByTagName(tag[0]); 
      nl2 = doc.getElementsByTagName(tag[1]); 
      if (nl1.getLength() > 0) { 
       list_of_geopoints = new ArrayList<GeoPoint>(); 
       for (int i = 0; i < nl1.getLength(); i++) { 
        Node node1 = nl1.item(i); 
        Node node2 = nl2.item(i); 
        double lat = Double.parseDouble(node1.getTextContent()); 
        double lng = Double.parseDouble(node2.getTextContent()); 
        list_of_geopoints.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6))); 
       } 
      } else { 
         // No points found 
        } 
    } 
} 

      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

      return list_of_geopoints; 
     } 

     @Override 
     protected boolean isRouteDisplayed() { 
      // TODO Auto-generated method stub 
      return false; 
     } 

任何人都可以給我一些想法如何改善這條路線,以及如何得到道路判決。目前我以千米爲單位獲得烏鴉飛行距離,但我需要通過公路獲得距離。

在此先感謝。

回答

0

如果你想獲得的所有點,你必須解析<polyline><points>[email protected]</points></polyline>,不僅需要<lat><lng>

Android Maps Utils會幫助你解析那個有趣的字符串。

+0

我不明白這一點。如何解析 a〜l〜Fjk〜uOwHJy @ P Guria

+0

嗯,我發現了一些代碼來解析點,但即使如此,我有問題,因爲它不是完整的路徑。它在目的地前幾米處結束。有任何想法嗎? – Guria

+0

@Guria在整個XML文件中有很多這樣的元素,所以將它們全部解析並連接在一起。 –

0
<html> 
    <head><title>dis</title></head> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js? 
    sensor=false&libraries=geometry"></script> 

    <script> 
    var p1 = new google.maps.LatLng(45.463688, 9.18814); 
    var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002); 

    alert(calcDistance(p1, p2)); 

    //calculates distance between two points in km's 
    function calcDistance(p1, p2) { 
     return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2)/1000).toFixed(2); 
    } 

    </script> 
    <body> 
    </body> 
    </html> 
+0

這是基本結構,您可以稍後使輸入的座標動態變爲靜態,此方法已針對位移進行了測試並在KM中給出了答案彈出窗口。 –

相關問題