Reputation: 75
I wanted to make an android app which provides the distance between two areas in a city.
The user will have two input fields where he must enter the two area names he would like to find the distance.
After he enters the area name. My app should display the distance.
How should I implement this?
Upvotes: 0
Views: 557
Reputation: 35569
First of all you need to find latitude and longitude of that area name.
Convert both address in Location
object, refer this.
Calculate distance by using distanceTo()
location1.distanceTo(location2);
Returns the approximate distance in meters between this location and the given location.
refer this for distanceTo() detail.
Upvotes: 1
Reputation: 1344
Use the Google Maps Directions API. You'll need to request the directions over HTTP. You can do this directly from Android, or via your own server.
For example, directions from Montreal to Toronto:
GET http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false
You'll end up with some JSON. In routes[].legs[].distance
, you'll get an object like this:
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
},
You can also get the polyline information directly from the response object.
for more detail visit here. Get the distance between two locations in android?
Upvotes: 0
Reputation: 1077
First get latitude and longitude of your start point and end point and using distanceBetween method of Location class calculate the distance here is link http://developer.android.com/reference/android/location/Location.html#distanceBetween(double,%20double,%20double,%20double,%20float[])
Upvotes: 0