Making a Location object in Android with latitude and longitude values
Solution 1
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);
Solution 2
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);
Solution 3
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.
Related videos on Youtube
fonduman
Updated on July 08, 2022Comments
-
fonduman almost 2 years
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)
-
Paul Thompson almost 11 yearsLook stackoverflow.com/questions/8049612/… for more
-
-
Subby over 9 years+1 as the comments in this answer, though abridged and simple, were in actual fact extremely helpful. Thank you @Exception Al
-
Utsav Gupta over 8 yearsReally 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
-
Jason Hartley over 8 yearsIf 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); }
-
zulkarnain shah over 6 yearsYa i had missed out writing 'd' and i wasn't able to create the location object. Silly small mistake
-
ban-geoengineering about 6 years@dst What's
"yourprovidername"
? -
Ewoks almost 6 yearswhy in unit tests it doesn't have any effect when I set Lat and Lon on Location object. Location object is somehow null !?!?
-
Alp Altunel over 4 years@ban-geoengineering leave empty "" not necessary.
-
ban-geoengineering over 4 yearsWhy
LocationManager.GPS_PROVIDER
and are there any alternatives might we consider? -
Vijay E over 4 yearsThere are 2 more, which is PASSIVE and NETWORK_PROVIDER. Here is nice explanation for why and what to use. stackoverflow.com/questions/6775257/…
-
ban-geoengineering over 4 yearsIf the
location
object is being created manually, though, wouldn't it be more appropriate to use an empty string rather thanLocationManager.GPS_PROVIDER
,LocationManager.PASSIVE
orLocationManager.NETWORK_PROVIDER
? -
Ewoks over 4 years@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