计算两个地理点之间的最短路径? [英] Calculate Shortest Path between two geo points?

查看:98
本文介绍了计算两个地理点之间的最短路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的Java和Android系统。我需要找到两个goepoint之间的最短路径。 我一直在寻找一整天的答案和我刚刚得到这个code:

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 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);
      }
    });
  }

主要code是在这里:

The main code is here :

      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);
              }
            });

现在的问题是我想实现这个code的Andr​​oid系统。但我不知道怎么办。 有没有人知道如何改变code这样我就可以在Android中使用它?

The problem is I want to implement this code in Android. but i dont know how. Is there anybody know how to change the code so i can use it in Android ?

下面的链接源<一href="http://gis.stackexchange.com/questions/25253/computing-shortest-distance-using-google-map-v3-and-openlayers">This

推荐答案

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

  1. 不使用互联网连接和放大器;使用了一些数学以计算两点之间的最短距离。

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

  • 使用互联网连接和放大器;使用谷歌地图API 以检测两个点之间的精确距离。在code实现是如下:

  • 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();
         }
      }
    }
    

  • 这篇关于计算两个地理点之间的最短路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆