Making a Location object in Android with latitude and longitude values
Asked Answered
I

4

163

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)

Isogloss answered 1/8, 2013 at 1:38 Comment(1)
Look #8050112 for moreThis
Q
363

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);
Quadruplet answered 1/8, 2013 at 1:52 Comment(8)
+1 as the comments in this answer, though abridged and simple, were in actual fact extremely helpful. Thank you @Exception AlTaxis
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 providerIngeborg
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); }Elma
Ya i had missed out writing 'd' and i wasn't able to create the location object. Silly small mistakeFlavius
why in unit tests it doesn't have any effect when I set Lat and Lon on Location object. Location object is somehow null !?!?Faxan
@Taxis 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 objectFaxan
Very nice. Better than the google documentation that's for sure.Fem
In kotlin you can use apply to make it look even better "val locationA = Location(String()).apply { latitude = 0.0d longitude = 0.0d }"Yugoslav
C
34

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);
Crazed answered 1/8, 2013 at 1:49 Comment(2)
@Crazed What's "yourprovidername"?Breechloader
@Breechloader leave empty "" not necessary.Transfuse
C
17

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.

Commination answered 13/6, 2018 at 6:7 Comment(3)
Why LocationManager.GPS_PROVIDER and are there any alternatives might we consider?Breechloader
There are 2 more, which is PASSIVE and NETWORK_PROVIDER. Here is nice explanation for why and what to use. #6775757Commination
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?Breechloader
M
2

Kotlin version:

val location = Location("")
location.latitude = 1.2345 // data type is Double
location.longitude = 1.2345 // data type is Double
Midday answered 30/5, 2023 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.