0

我是 Java 和 Android 的新手。我需要找到两个 goepoint 之间的最短路径。我整天都在寻找答案,我刚刚得到了这个代码:

var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
  }

  function calcRoute() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        routePath = result.routes[0].overview_path;
        for(var a = 0; a< routePath.length; a++){
          // Your vector layer to render points with line
        }
        directionsDisplay.setDirections(response);
      }
    });
  }

主要代码在这里:

      directionsService.route(request, function(response, status) {
              if (status == google.maps.DirectionsStatus.OK) {
                routePath = result.routes[0].overview_path;
                for(var a = 0; a< routePath.length; a++){
                  // Your vector layer to render points with line
                }
                directionsDisplay.setDirections(response);
              }
            });

问题是我想在 Android 中实现这段代码。但我不知道怎么做。有没有人知道如何更改代码以便我可以在 Android 中使用它?

这里是链接源这个

4

1 回答 1

2

有两种方法可以找到两个 GeoPoints 之间的距离:

  1. 不使用互联网连接并使用一些数学来计算两点之间的最短距离。

    /**** 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. 使用互联网连接并使用 Google Maps API来检测两点之间的确切距离。实现它的代码如下:

    /**** 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();
         }
      }
    }
    
于 2013-10-14T07:36:42.567 回答