Calculate Shortest Path between two geo points?

后端 未结 1 501
悲哀的现实
悲哀的现实 2021-01-16 20:54

Im a new in Java and Android. I need to find Shortest Path between two goepoint. I have been searching all day for the answer and i just got this code :

var          


        
相关标签:
1条回答
  • 2021-01-16 21:45

    There are two ways to find distance between two GeoPoints :

    1. This does not use internet connection & uses some mathematics to calculate the shortest distance between the two points.

      /**** Method for Calculating distance between two locations ****/
      public float DistanceBetweenPlaces(double lat1, double lon1, double lat2, double lon2, Context cont) {
          float[] results = new float[1];
          Location.distanceBetween(lat1, lon1, lat2, lon2, results);
          return results[0];
      }
      
    2. This uses internet connection & uses Google Maps API to detect the exact distance between the two points. The code implementing it is below :

      /**** Class for calculating the distance between two places ****/
      public class CalculateDistance {
           String distance;
           public void calculate_distance(String src_lat, String src_lng, String dest_lat, String dest_lng) {
               distance = getdistance(makeUrl(src_lat, src_lng, dest_lat, dest_lng));
           }
      
      /**** Method for make URL ****/
      private String makeUrl(String src_lat, String src_lng, String dest_lat, String dest_lng) {
          StringBuilder urlString = new StringBuilder();
          urlString.append("http://maps.googleapis.com/maps/api/distancematrix/json?");
          urlString.append("origins="); // from
          urlString.append(src_lat);
          urlString.append(",");
          urlString.append(src_lng);
          urlString.append("&destinations="); // to
          urlString.append(dest_lat);
          urlString.append(",");
          urlString.append(dest_lng);
          urlString.append("&sensor=true&mode=driving");
          return urlString.toString();
      }
      
      /**** Method for getting the distance between the two places ****/
      private String getdistance(String urlString) {
           URLConnection urlConnection = null;
           URL url = null;
           try {
                url = new URL(urlString.toString());
                urlConnection = url.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
      
                String line;
                StringBuffer sb = new StringBuffer();
                // take Google's legible JSON and turn it into one big string.
                while ((line = in.readLine()) != null) {
                      sb.append(line);
                }
                // turn that string into a JSON object
                JSONObject distanceMatrixObject = new JSONObject(sb.toString());
                // now get the JSON array that's inside that object
                if (distanceMatrixObject.getString("status").equalsIgnoreCase("OK")) {
                      JSONArray distanceArray = new JSONArray(distanceMatrixObject.getString("rows"));
                      JSONArray elementsArray = new JSONArray(distanceArray.getJSONObject(0).getString("elements"));
                      JSONObject distanceObject = new JSONObject(elementsArray.getJSONObject(0).getString("distance"));
                      return distanceObject.getString(("text"));
                }
           return null;
           } catch (Exception e) {
                return null;
                e.printStackTrace();
           }
        }
      }
      
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题