159

I have a program in which latitude and longitude values of a location are stored in a database, which I download.

I want to get the distance between these coordinates, and my current location.

The Location class has a simple method to find the distance between two Location objects, so I figured I'd make a Location object with the coordinates, then call the method.

Is there an easy way to do this? Also, if there's another reliable, fairly simple equation that won't clutter things too much, that would work too. Thanks.

(android.location.Location)

fonduman
  • 1,655
  • 3
  • 11
  • 11

4 Answers4

354

Assuming that you already have a location object with your current location.

Location targetLocation = new Location("");//provider name is unnecessary
targetLocation.setLatitude(0.0d);//your coords of course
targetLocation.setLongitude(0.0d);

float distanceInMeters =  targetLocation.distanceTo(myLocation);
Androiderson
  • 16,865
  • 6
  • 62
  • 72
  • 1
    +1 as the comments in this answer, though abridged and simple, were in actual fact extremely helpful. Thank you @Exception Al – Subby Aug 26 '14 at 10:05
  • 3
    Really bad example of documentation for Android, wasted quite some time on this. My other question is suppose I provide a valid provider like gps, will it give me the current location? In other words what is the significance of provider – Utsav Gupta Oct 10 '15 at 14:03
  • If you need a default value for a Location object and since you can't initialize it with a lat/lon, I suggest setting them in a static initializer, e.g. `static { location.setLatitude(0.0d); location.setLongitude(0.0d); }` – Jason Hartley Jan 26 '16 at 19:24
  • Ya i had missed out writing 'd' and i wasn't able to create the location object. Silly small mistake – zulkarnain shah Oct 18 '17 at 04:20
  • 1
    why in unit tests it doesn't have any effect when I set Lat and Lon on Location object. Location object is somehow null !?!? – Ewoks Jun 21 '18 at 10:23
  • @Subby because Location is Android framework class (so it is fake -> it's null by default in vanilla unit tests), but if you wrap it in some interface, you can mock it in tests and implementation in production can use Android location object – Ewoks Nov 20 '19 at 13:25
  • Very nice. Better than the google documentation that's for sure. – dcarl661 Jul 07 '22 at 20:49
  • In kotlin you can use apply to make it look even better "val locationA = Location(String()).apply { latitude = 0.0d longitude = 0.0d }" – Iglesias Leonardo Aug 30 '23 at 09:55
32

You may create locations using their constructor, then set the latutude and longitude values.

final Location location = new Location("yourprovidername");
location.setLatitude(1.2345d);
location.setLongitude(1.2345d);
dst
  • 3,307
  • 1
  • 20
  • 27
15

I am answering this again, because lot of people like me do not know what "providername" actually is. Below code answers the question:

Location location = new Location(LocationManager.GPS_PROVIDER);
location.setLatitude(23.5678);
location.setLongitude(34.456);

Here I am using LocationManager.GPS_PROVIDER as the provider name.

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
Vijay E
  • 829
  • 10
  • 14
  • Why `LocationManager.GPS_PROVIDER` and are there any alternatives might we consider? – ban-geoengineering Nov 07 '19 at 14:12
  • 1
    There are 2 more, which is PASSIVE and NETWORK_PROVIDER. Here is nice explanation for why and what to use. https://stackoverflow.com/questions/6775257/android-location-providers-gps-or-network-provider – Vijay E Nov 08 '19 at 11:35
  • 2
    If the `location` object is being created manually, though, wouldn't it be more appropriate to use an empty string rather than `LocationManager.GPS_PROVIDER`, `LocationManager.PASSIVE` or `LocationManager.NETWORK_PROVIDER`? – ban-geoengineering Nov 09 '19 at 20:26
0

Kotlin version:

val location = Location("")
location.latitude = 1.2345 // data type is Double
location.longitude = 1.2345 // data type is Double
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256