Reputation: 71
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);
}
});
}
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);
}
});
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 ?
Here the link source This
Upvotes: 0
Views: 2623
Reputation: 11915
There are two ways to find distance between two GeoPoints :
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];
}
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();
}
}
}
Upvotes: 2