Reputation: 1099
There is the Location.distanceBetween method, but that's just for direct line. I have also looked into The Google Distance Matrix API, but that seems rather involved although it would give me the information I need. Are there any other ways?
Upvotes: 0
Views: 196
Reputation: 7114
As far as I know, no. Distance Matrix is the only service from Google that will provide you with the information you need (Car, Walking, Bicycle distance between two points).
The usage is simple enough. I've used it several times (JSON) and it works very well. Here is the code to get the distance:
String sDistance = "";
try
{
URL googleDMatrix = new URL("http://maps.googleapis.com/maps/api/distancematrix/json?origins="
+ URLEncoder.encode(params[6].toString(), "UTF-8") + "&destinations="
+ URLEncoder.encode(params[7].toString(), "UTF-8") + "&language=en-GB&sensor=false&units=imperial");
URLConnection tc = googleDMatrix.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.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 main = new JSONObject(sb.toString());
// now get the JSON array that's inside that object
JSONArray rows_array = new JSONArray(main.getString("rows"));
JSONObject elements_object = rows_array.getJSONObject(0);
JSONArray elements_array = elements_object.getJSONArray("elements");
JSONObject distance_object = elements_array.getJSONObject(0);
JSONObject distance = distance_object.getJSONObject("distance");
double dist = (distance.getDouble("value") / 1E3) * 0.62;
sDistance = Double.toString(dist);
}
catch (Exception e)
{
sDistance = "0";
e.printStackTrace();
}
There might be a couple of extra things in here since this is straight from my project. params[6] params[7] are the two points between which the distance will be calculated.
Upvotes: 1