我想獲取兩個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;
}
任何人都可以給我一些想法如何改善這條路線,以及如何得到道路判決。目前我以千米爲單位獲得烏鴉飛行距離,但我需要通過公路獲得距離。
在此先感謝。
謝謝。你的答案幫助我獲得了距離 – Guria