Android 如何获得两个地质点坐标之间的最短行驶路径和距离?
我想得到两个地质点坐标之间的道路距离。目前,我得到的路径,但它不是非常准确,因为它考虑的是道路的起点和终点,而不是道路中的曲线。我正在使用以下代码:Android 如何获得两个地质点坐标之间的最短行驶路径和距离?,android,google-maps,latitude-longitude,driving-directions,geopoints,Android,Google Maps,Latitude Longitude,Driving Directions,Geopoints,我想得到两个地质点坐标之间的道路距离。目前,我得到的路径,但它不是非常准确,因为它考虑的是道路的起点和终点,而不是道路中的曲线。我正在使用以下代码: Double sLat = Double.parseDouble(getIntent().getStringExtra("sLat")); Double sLon = Double.parseDouble(getIntent().getStringExtra("sLon")); Double dLat = Double.parseDouble(ge
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;
}
有谁能给我一些建议,如何改进这条路线,以及如何通过公路获得距离。目前我得到的乌鸦飞行距离以公里为单位,但我需要的是通过公路获得距离
提前感谢。您可能需要使用
例如,这里是法国巴黎和德国柏林之间的车程
如果你想得到所有的点,你必须解析a~l~Fjk~uOwHJy@P而不仅仅是拿和
将帮助您解析这个有趣的字符串。谢谢。你的回答帮助我获得了距离,我不明白重点。如何解析a~l~Fjk~uOwHJy@PWell我已经找到了一些解析点的代码,但即使这样,我还是有问题,因为它不是完整的路径。它在距离目的地仅几米远的地方结束。有什么想法吗?@Guria在整个xml文件中有很多这样的元素,所以将它们全部解析并连接在一起。这是基本结构,您可以在以后使输入的坐标从静态变为动态,该方法经过位移测试,并以KMs弹出答案。
<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>