gpt4 book ai didi

安卓谷歌地图查找距离

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:31 25 4
gpt4 key购买 nike

我正在尝试找出两个位置之间的距离。我有经度和纬度,我可以计算欧氏距离。但我想找到道路距离。我的意思是,我想计算从源头到目的地时我要走的路的距离。在这种情况下如何计算呢?

最佳答案

最简单的方法是使用 Google Directions API 获取路线,这会为您提供沿途所有点的列表(以及总距离)。

checkout :http://code.google.com/apis/maps/documentation/directions/

如果您不确定如何执行此操作,请告诉我,我会为您发布一些代码,这些代码将从谷歌获取指示并提取您需要的数据。

更新 - 要求的代码

private void GetDistance(GeoPoint src, GeoPoint dest) {

StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
urlString.append("origin=");//from
urlString.append( Double.toString((double)src.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append( Double.toString((double)src.getLongitudeE6() / 1E6));
urlString.append("&destination=");//to
urlString.append( Double.toString((double)dest.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append( Double.toString((double)dest.getLongitudeE6() / 1E6));
urlString.append("&mode=walking&sensor=true");
Log.d("xxx","URL="+urlString.toString());

// get the JSON And parse it to get the directions data.
HttpURLConnection urlConnection= null;
URL url = null;

url = new URL(urlString.toString());
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();

InputStream inStream = urlConnection.getInputStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));

String temp, response = "";
while((temp = bReader.readLine()) != null){
//Parse data
response += temp;
}
//Close the reader, stream & connection
bReader.close();
inStream.close();
urlConnection.disconnect();

//Sortout JSONresponse
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
JSONArray array = object.getJSONArray("routes");
//Log.d("JSON","array: "+array.toString());

//Routes is a combination of objects and arrays
JSONObject routes = array.getJSONObject(0);
//Log.d("JSON","routes: "+routes.toString());

String summary = routes.getString("summary");
//Log.d("JSON","summary: "+summary);

JSONArray legs = routes.getJSONArray("legs");
//Log.d("JSON","legs: "+legs.toString());

JSONObject steps = legs.getJSONObject(0);
//Log.d("JSON","steps: "+steps.toString());

JSONObject distance = steps.getJSONObject("distance");
//Log.d("JSON","distance: "+distance.toString());

String sDistance = distance.getString("text");
int iDistance = distance.getInt("value");

}

这将返回两点之间的距离,您可以更改函数以将其作为字符串或整数返回,只需返回 sDistance 或 iDistance。

希望这对你有帮助,如果你需要更多帮助,请告诉我。

关于安卓谷歌地图查找距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6456090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com