【问题标题】:Distance calculation from my location to destination location in android从我的位置到android中目标位置的距离计算
【发布时间】:2012-05-27 20:28:53
【问题描述】:

我必须制作一个项目,我需要计算从我的位置到目标位置的距离并将其显示在文本视图中。请注意,当我的位置更改时,此距离会更新。是否可以制作这种类型的项目?

[注意:没有实现谷歌地图,我必须制作它。目的地 lon ,lat 是已知的。只需找到我的位置并进行计算]

【问题讨论】:

    标签: android location distance


    【解决方案1】:

    查看 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];
    }
    

    【讨论】:

    • 你可以开始一个新的异步任务,当获得一个新的位置时(见上面的链接)。在 onpostexecute 方法中,您可以在 textview 中写入距离。
    • 但是怎么办?我很抱歉。我是 android 的新手。请您在这里解释一下或给我代码以在 textview 中显示它吗?
    • 你创建了一个监听器(见链接)。在 onLocationChanged() 方法中执行一个新的 AsyncTask。此任务需要在 textview (-> findViewById()) 上有一个引用。在任务的 doInBackground() 方法中计算距离,在 onPostExecute() 方法中设置文本视图的文本。也许有一种更优雅的存档方式,但它有效:)
    • 如果你想要以 km 为单位的结果,也请用 1000 除以结果,例如 "return resultArray[0]/1000;"
    【解决方案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;
    
         }
    

    }

    【讨论】:

      【解决方案3】:

      我已经在 this Question 中写下了 2 个不同的答案来计算两个地理点之间的差异,所以不要在这里复制粘贴,

      第二件事也参见 this Example,与您需要实现 Locationlistener 以获取 onLocationChanged 事件的方式相同。

      在这种情况下,使用上面给定的函数计算差异并适当地使用它们。

      【讨论】:

        【解决方案4】:

        使用LocationServices 确定您的位置并在每次收到更新时使用新距离更新您的TextView 非常简单。

        【讨论】:

          猜你喜欢
          • 2011-04-03
          • 1970-01-01
          • 2012-01-16
          • 2017-04-20
          • 1970-01-01
          • 2018-12-04
          • 1970-01-01
          • 2013-06-02
          • 1970-01-01
          相关资源
          最近更新 更多