Making a Location object in Android with latitude and longitude values

97,418

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.

Share:
97,418

Related videos on Youtube

fonduman
Author by

fonduman

Updated on July 08, 2022

Comments

  • fonduman
    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)

  • Subby
    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
    Utsav Gupta over 8 years
    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
  • Jason Hartley
    Jason Hartley over 8 years
    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); }
  • zulkarnain shah
    zulkarnain shah over 6 years
    Ya i had missed out writing 'd' and i wasn't able to create the location object. Silly small mistake
  • ban-geoengineering
    ban-geoengineering about 6 years
    @dst What's "yourprovidername"?
  • Ewoks
    Ewoks almost 6 years
    why 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
    Alp Altunel over 4 years
    @ban-geoengineering leave empty "" not necessary.
  • ban-geoengineering
    ban-geoengineering over 4 years
    Why LocationManager.GPS_PROVIDER and are there any alternatives might we consider?
  • Vijay E
    Vijay E over 4 years
    There 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
    ban-geoengineering over 4 years
    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?
  • Ewoks
    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