0
我一個計步器程序的工作像下面如何計算android中的步行距離?
到目前爲止,我發現像下面的一些解決方案:
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數據的間隔時間有多長。
感謝您的幫助
我應該多頻繁地獲取GPS數據? – user782104
30秒是最好的一個。你可以設定哪一個適合你。只有當你得到新的位置更新時,你需要計算距離。 –