从我的位置距离计算在Android的目标位置从我的位置距离计算在Android的目标位置(Dista

2019-05-14 09:38发布

我对做一个项目,我需要从我的位置计算距离目标位置,并显示在一个textview.Here注意,这个距离的时候更新我的位置change.Is有可能使这种类型的项目?

[注:不执行谷歌地图我必须做出后援目的地经度,纬度是known.Just要找到我的位置,并计算]

Answer 1:

检查谷歌Android开发人员网页上的文档,以了解如何收听位置的变化。 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];
}


Answer 2:

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;


} 

}

这会给你的路径/公路的距离或长度两点之间。

这里是解析JSON API的parser_Json类

 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;

     }

}



Answer 3:

我在这里写下来2个不同的答案, 这个问题来计算两个地理点之间的差异,从而不能复制粘贴在这里,

第二件事情,也看到这个例子 ,你需要实现同样的方式Locationlistener获得onLocationChanged事件。

而在这种情况下计算差别使用前面介绍的功能和适当地使用它们。



Answer 4:

这是使用相当简单LocationServices来确定你的位置和更新TextView与新的距离每次接收更新时间。



文章来源: Distance calculation from my location to destination location in android