"How to get the actual road distance between two places both the points are fixed"



我们正在创建一个Android应用程序,我们需要计算两个固定点之间的确切道路距离。我们需要道路距离而不是Arial距离(鸟飞行距离(。我们需要在旅行开始前计算估计的行程费用。

提前谢谢。 所有的答案和帮助将不胜感激。

使用 goolge API

public float getDistance(double lat1, double lon1, double lat2, double lon2) {
String result_in_kms = "";
String url = "http://maps.google.com/maps/api/directions/xml?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric";
String tag[] = {"text"};
HttpResponse response = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
response = httpClient.execute(httpPost, localContext);
InputStream is = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(is);
if (doc != null) {
NodeList nl;
ArrayList args = new ArrayList();
for (String s : tag) {
nl = doc.getElementsByTagName(s);
if (nl.getLength() > 0) {
Node node = nl.item(nl.getLength() - 1);
args.add(node.getTextContent());
} else {
args.add(" - ");
}
}
result_in_kms =String.valueOf( args.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
Float f=Float.valueOf(result_in_kms);
return f*1000;
}

这是第一个答案的变体。第一次调用getDocument,其中包含开始和结束位置,模式(驾驶,公交,骑自行车,步行(,语言。 然后将该文档传递给 getTurnByTurn((。 这将返回行程的"步骤"或段数组,前一步的终点和新步骤的终点之间的距离。可能以公里为单位,必要时需要转换为英里。

public Document getDocument(String start, String dest, String mode, String language) {
try {
start = URLEncoder.encode(start, "utf-8");
dest = URLEncoder.encode(dest, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
long milliseconds = System.currentTimeMillis();
long seconds = milliseconds/1000;
String url = "https://maps.googleapis.com/maps/api/directions/xml?departure_time="
+ seconds
+ "&origin=" + start
+ "&destination=" + dest
+ "&language=" + language
+ "&sensor=false&mode=" + mode;
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;
}
public ArrayList<SearchItem> getTurnByTurn (Document doc) {
NodeList nl1, nl2, nl3;
ArrayList<SearchItem> listDirections = new ArrayList<SearchItem>();
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 distanceNode = nl2.item(getNodeIndex(nl2, "distance"));
nl3 = distanceNode.getChildNodes();
Node textNode = nl3.item(getNodeIndex(nl3, "text"));
String distance = textNode.getTextContent();
Node durationNode = nl2.item(getNodeIndex(nl2, "duration"));
nl3 = durationNode.getChildNodes();
textNode = nl3.item(getNodeIndex(nl3, "text"));
String duration = textNode.getTextContent();
Node instructionsNode = nl2.item(getNodeIndex(nl2, "html_instructions"));
String instructions = instructionsNode.getTextContent();
String details = distance + " -- " + duration;

listDirections.add(new SearchItem(instructions, details, "", false));
}
}
return listDirections;
}

相关内容

最新更新