-1

I write an android app

And I get from the server a list of addresses.

How can I measure the distance between a mobile client and a given address?

I guess I first have to convert the address to coordinates? how would you suggest doing this?

Elad Benda2
  • 9,463
  • 23
  • 67
  • 138

6 Answers6

2

You can get lat/lng using address.

Code for fetching lat/lng from given address :

 Geocoder coder = new Geocoder(this);
 List<Address> address;

 try {
   address = coder.getFromLocationName(strAddress,5);
   if (address == null) {
       return null;
   }
   Address location = address.get(0);
   lat = location.getLatitude(); // get lat
   lng = location.getLongitude();  // get lng


  return p1;
  }

after this you can fetch your current location.

Finally you need to use distanceTo() method, pass all these src/dest lat/lng value so you will get distance between 2 places.

VVB
  • 6,526
  • 7
  • 39
  • 71
1

First, if you have control over the database MongoDB has some pretty good sorting algorithms for location based stuff. Depending on if the addresses you are getting are the same or different you can also insert the lat/long into the db with the address so that you don't have to look it up each time.

That being said, you can use the Google Geocoding API to convert an address into a lat and long. You can find a decent example here using PHP using cURL: http://www.techrecite.com/get-latitude-longitude-of-an-address-using-google-geocoding-api/

Combine that with using java.net.Connection, a great explanation here: Using java.net.URLConnection to fire and handle HTTP requests

And you are on your way.

Community
  • 1
  • 1
michaelp
  • 323
  • 5
  • 23
0

you can set LocationListner and get current Lat-Log of mobile user and then simply use distanceTo method of Location class.

Check this links:

1) http://developer.android.com/reference/android/location/LocationManager.html

2) http://developer.android.com/reference/android/location/LocationListener.html

3) http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ (Example)

I your server gives you address as Lat-Long then this will help you for sure.

Note Add : <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in your manifest

DCoder
  • 3,304
  • 7
  • 32
  • 64
0

I think a more useful thing would be to get direction from the Google Directions API

Follow the directions to enable it and get a key. Then try something like this in your browser:

http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=API_KEY

You get a tonne of info back. This includes coords:

"bounds" : { "northeast" : { "lat" : 45.5110114, "lng" : -73.5530088 }, "southwest" : { "lat" : 43.6533103, "lng" : -79.3827675 } but also includes route length. This is the walking distance:

"legs" : [ { "distance" : { "text" : "542 km", "value" : 542358 },

There we go, I can get bird-flys distance from this (although maybe this api is overkill if that's your only question), but I can get the arguably more useful info of how long the best route is.

Nathan Cooper
  • 5,499
  • 4
  • 32
  • 60
0

Hope this help:

https://developers.google.com/maps/documentation/distancematrix/

Have in mind this:

Distance Matrix API must relate to the display of information on a Google Map; for example, to determine origin-destination pairs that fall within a specific driving time from one another, before requesting and displaying those destinations on a map. Use of the service in an application that doesn't display a Google map is prohibited.

SilentTremor
  • 4,279
  • 2
  • 14
  • 26
0

The first, essential step is to find the users Location. To do this, check out http://developer.android.com/guide/topics/location/strategies.html

For different strategies on how to do this.

You'll end up with a Location object of the users location.

The next step is to get the coordinates of the address. To do this, you will need to use the Geocoder class. Create a new instance of it, and use "getFromLocationName("address", 1).get(0)" to get a Location object which is the closest result to "address" (replace "address" with whatever address you have from the server).

You may need to play around with this to get the right result.

The final step is easy, you can use the "usersLocation.distanceTo(addressLocation)", with "usersLocation" and "addressLocation" being the location object for the user and the remotely obtained address respectively.

This will give you a float, which is the distance in metres.

Moony22
  • 16
  • 3