Calculer le chemin le plus court entre deux points géo?

Je suis un nouveau en Java et Android. Je dois trouver le chemin le plus court entre deux goepoint. J'ai cherché toute la journée pour la réponse et je viens d'obtenir ce 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); } }); } 

Le code principal est ici:

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

Le problème est que je veux implémenter ce code dans Android. Mais je ne sais pas comment. Y a-t-il quelqu'un qui sait comment changer le code afin que je puisse l'utiliser dans Android?

Ici, la source du lien This

Il existe deux façons de trouver une distance entre deux GeoPoints:

  1. Cela n'utilise pas la connexion Internet et utilise des mathématiques pour calculer la distance la plus courte entre les deux 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. Cela utilise la connexion Internet et utilise l'API Google Maps pour détecter la distance exacte entre les deux points. Le code de mise en œuvre est ci-dessous:

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