gpt4 book ai didi

android - 如何计算android中的步行距离?

转载 作者:行者123 更新时间:2023-11-29 16:02:36 26 4
gpt4 key购买 nike

我正在开发如下所示的计步器应用

enter image description here

到目前为止,我找到了如下解决方案:

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");

}

我决定用GPS的方式来实现。但是,实现这个的“常用方法”是什么?是否只是在 30 秒获取 gps lat,lan,然后计算差异?例如获取 GPS 数据的间隔时间是多长。

感谢帮助

最佳答案

您可以使用计算距离的方法来代替调用 google api。

public double getDistance(double lat1, double lon1, double lat2, double lon2) {
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
(Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang *6371;
return dist;
}

这将减少一次 API 调用并快速找到两个位置之间的距离。

关于android - 如何计算android中的步行距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22653997/

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