Making a Location object in Android with latitude and longitude values

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.


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

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