我必須做一個項目,我需要計算從我的位置到目標位置的距離,並在textview中顯示它。請注意,當我的位置更改時更新此距離。是否有可能製作這種類型的項目?從我的位置到目標位置的距離計算android
[注:不執行谷歌地圖我必須做出後援目的地經度,緯度是known.Just必須找到我的位置,並計算]
我必須做一個項目,我需要計算從我的位置到目標位置的距離,並在textview中顯示它。請注意,當我的位置更改時更新此距離。是否有可能製作這種類型的項目?從我的位置到目標位置的距離計算android
[注:不執行谷歌地圖我必須做出後援目的地經度,緯度是known.Just必須找到我的位置,並計算]
查看google android dev頁面上的文檔,瞭解如何偵聽位置變化。 http://developer.android.com/guide/topics/location/obtaining-user-location.html
您可以使用此功能來確定當前(開始)點和目標點之間的距離。
/**
* using WSG84
* using the Metric system
*/
public static float getDistance(double startLati, double startLongi, double goalLati, double goalLongi){
float[] resultArray = new float[99];
Location.distanceBetween(startLati, startLongi, goalLati, goalLongi, resultArray);
return resultArray[0];
}
我在這裏2個不同的答案寫下來this Question計算兩個地理點之間的差異,從而不能複製,在此粘貼,
第二件事也看到this Example你需要以同樣的方式執行Locationlistener
即可獲得onLocationChanged
事件。
並在這種情況下使用上述給定函數計算差異並適當地使用它們。
Location.distanceBetween
會給兩點之間的直線距離。如果你想兩個地理點之間PATH的距離,那麼你可以使用通過使用此類做到這一點:
public class GetDistance {
public String GetRoutDistane(double startLat, double startLong, double endLat, double endLong)
{
String Distance = "error";
String Status = "error";
try {
Log.e("Distance Link : ", "http://maps.googleapis.com/maps/api/directions/json?origin="+ startLat +","+ startLong +"&destination="+ endLat +","+ endLong +"&sensor=false");
JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/directions/json?origin="+ startLat +","+ startLong +"&destination="+ endLat +","+ endLong +"&sensor=false");
Status = jsonObj.getString("status");
if(Status.equalsIgnoreCase("OK"))
{
JSONArray routes = jsonObj.getJSONArray("routes");
JSONObject zero = routes.getJSONObject(0);
JSONArray legs = zero.getJSONArray("legs");
JSONObject zero2 = legs.getJSONObject(0);
JSONObject dist = zero2.getJSONObject("distance");
Distance = dist.getString("text");
}
else
{
Distance = "Too Far";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Distance;
}
}
這會給兩點之間的距離,你或路徑/道路的長度。
這裏是parser_Json類解析JSON API
public class parser_Json {
public static JSONObject getJSONfromURL(String url){
//initialize
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//try parse the string to a JSON object
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
public static InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
}
return null;
}
}
我該怎麼讓這個結果的TextView? – MBMJ
你可以開始一個新的asynctask,當一個新的職位獲得(見上面的鏈接)。在onpostexecute方法中,您可以在textview中寫入距離。 – CAA
但是怎麼樣?我很抱歉,我是android中的新手。請你在這裏解釋一下,或者給我代碼在textview中顯示它? – MBMJ