I am making a map app in android, I want to get 2 locations on the map, and see the distance between them. I already got my current location as a "Location" variable. The other place however is saved as two variables : double lat,lng;
I have checked the internet and found this method that will help :
float distance = myLocation.distanceTo(temp);
The problem is that the "temp" I have is not a "Location", it is 2 different doubles.
Is there a way to convert them to Location?
PS. Code i tried but did't work :
Location temp = null;
temp.setLatitude(23.5678);
temp.setLongitude(34.456);
float distance = location.distanceTo(temp);
Problem :
Null pointer access: The variable temp can only be null at this location
Reverse geocoding is the process to convert the latitude and longitude coordinates to a readable address. Type the lat long coordinates and press Convert button. Reverse geocoded address will shown below. Also the municipality, subdivision and country name can be found.
From the GPS coordinates, the geohash is calculated by creating two binary strings, one for the longitude and one for the latitude, by dichotomy of the corresponding intervals. By splitting each interval into 2 equal parts and noting 0 the left interval and 1 the right interval.
You are correct, there is no universal standard on the order: In mathematical functions which do an universal conversion, between x,y or lon,lat or inverse, the lon,lat order should be used, because the x-axis relates to longitude and y to latitude and the x,y order is usually preferred.
You have to instantiate Location
, before accessing its members. For example
Location temp = new Location(LocationManager.GPS_PROVIDER);
temp.setLatitude(23.5678);
temp.setLongitude(34.456);
float distance = location.distanceTo(temp);
val distance = location.distanceTo(Location(LocationManager.GPS_PROVIDER).apply {
latitude = 23.5678
longitude = 34.456
})
Alternatively, you can get the distance without instantiating a Location object at all using the static method Location.distanceBetween().
float[] results = new float[1];
Location.distanceBetween(1, 2, 2 , 2, results);
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results1. If results has length 3 or greater, the final bearing is stored in results[2].
Parameters
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With