fonduman
fonduman

Reputation: 1725

Making a Location object in Android with latitude and longitude values

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)

Upvotes: 165

Views: 106565

Answers (4)

Lance Samaria
Lance Samaria

Reputation: 19572

Kotlin version:

val location = Location("")
location.latitude = 1.2345 // data type is Double
location.longitude = 1.2345 // data type is Double

Upvotes: 2

Androiderson
Androiderson

Reputation: 17083

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);

Upvotes: 364

Vijay E
Vijay E

Reputation: 958

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.

Upvotes: 17

dst
dst

Reputation: 3337

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);

Upvotes: 35

Related Questions