i am using geolocator: ^7.3.1 package They mentioned a function that calculates the distance between two geographical points , But the result comes in meters by default
How could this be done in Km
getDistance(){
double distanceInMeters = Geolocator.distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);
print(distanceInMeters); // result comes in meters by default which is 144851.67191816124 meters
}
How can I get the result in kilometers, and in short number not like that long number in their example?
Conversion
double distanceInMeters = 144851.67191816124;
double distanceInKiloMeters = distanceInMeters / 1000;
double roundDistanceInKM =
double.parse((distanceInKiloMeters).toStringAsFixed(2));
print(distanceInMeters);
print(distanceInKiloMeters);
print(roundDistanceInKM);
Output
144851.67191816124
144.85167191816123
144.85
Is this helpful?
Related
I have 2 coordinates - coordinate consisting of long and lat which is one location and coordinate b which is another location. How do I correctly convert to two distances into miles - for example, I want it to display 2.3 miles away based on my calculation. I think my calculation may be wrong, as I am getting values like 2339.32? Am I not rounding off correctly or is my calculation wrong?
let userCoordinates = CLLocation(latitude: userLatitude, longitude: userLongitude)
let locationCoordinates = CLLocation(latitude: locationLatitude, longitude:
locationLongitude)
let distanceInMeters = userCoordinates.distance(from: locationCoordinates) // gets the distance from both locations.
let miles = distanceInMeters * 0.62137
I have a number that I am trying to append to an array. The number is a coordinate -37.77745068746633 however in my Swift project if I println the array after the append call the value that should be -37.77745068746633 is -37.77745069
I am receiving the number through Google snap to roads API and I can see the original as the full number but changes after I call
self.latitude = locations["latitude"] as! Double
Swift just doesn't seem to store the entire value. Is there rounding on by default?
Thanks
If you keep the number as Double all the time, Swift won't round it, it is most likely the print statement that truncates it to a reasonable precision.
By the way, try to check the distance between the original and the truncated coordinates:
let lat1: Double = -37.77745068746633
let lat2: Double = -37.77745069
let lon: Double = 150
let loc1 = CLLocation(latitude: lat1, longitude: lon)
let loc2 = CLLocation(latitude: lat2, longitude: lon)
print(loc1.distanceFromLocation(loc2)) // prints 0.000281218294500339
If I am not making any mistake, the difference is just 0.3 millimeters.
I am using PostgreSQL to store the location of a user send to the server by my android app. I needed to find the total distance travelled by the user for a particular time duration.
The user location is stored in the following table :
CREATE TABLE userlocation
(
latitude character varying,
longitude character varying,
geopoint point,
userid integer,
locationtime timestamp
)
I retrieved the records and calculated the distance in java using the following haversine distance method :
public double getdistance(final double lat1, final double lon1, final double lat2, final double lon2, final char unit) {
final double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
if (Double.isNaN(dist)) {
dist = 0.0;
}
return (dist);
}
However this calculation is time consuming especially while calculating the distance for multiple days as there are a lot of records. I decided to try doing the distance calculation at the database level to reduce the calculation time. I found the the following query which allows me to calculate the distance to a certain point :
SELECT latitude, longitude, geopoint <-> '19.23,72.89' AS distance FROM userlocation ORDER BY distance;
I tried to create a query that would either return the total distance traveled or atleast calculate the distance between two consecutive rows and store it in another column so that I calculate the sum in Java instead of the distance calculations.
I have tried searching for a solution but I have been unable to find one yet. Most of the questions on SO deal with distance calculation between two points.
I do not have PostGIS at the moment. Would it be possible to calculate distance in PostgreSQL or should I just continue with my current approach? In that case is there an alternative for reducing the distance calculation time.
I had the same problem last month.
I added the module Earthdistance to PostgreSQL. This plugin add functions to compute the great circle distances between two points.
Installation is simple:
CREATE EXTENSION "cube";
CREATE EXTENSION "earthdistance";
Hope that helps
My Question is how can i find minimum and maximum latitude and longitude of specific area (500 meter) from current location.
In my case, Such like i need to get X and Y CLLocation (latitude and longitude) from 500meter of area
See my image (sorry for this may be bad drawing )
I also have to tried to googling and i got link such like
How can i get minimum and maximum latitude and longitude using current location and radius?
But i don't know how it implement in my case.
Pleas help me in this issue.
NOTE : I do not want to use CLLocationDistance distance = [currentLocation distanceFromLocation:newLocation]; because it is not helpful in my case so..
If you don't need a really precise value, then use the approximation that 1 degree is 111 km. Based on this, you need to add and remove 0.0025 degrees to the current coordinates to get corners of the area you are looking for.
rectanglesidelengthmeters = 500
degreedeltalat = 0.001 * (rectanglesidelengthmeters / 2.0) * cos(current.lon)
degreedeltalon = 0.001 * (rectanglesidelengthmeters / 2.0) * cos(current.lat)
minlat = current.lat - degreedeltalat
maxlat = current.lat + degreedeltalat
minlon = current.lon - degreedeltalon
maxlon = current.lon + degreedeltalon
You may need to correct the result a little for staying in the -90 .. 90 range for latitude and -180 .. 180 range for longitude values but I think CLClocation will handle that for you too.
You have to do some radius calculation from current location in km.
double kilometers = 0.5;
double curve = ABS( (cos(2 * M_PI * location.coordinate.latitude / 360.0) ));
MKCoordinateSpan span;
span.latitudeDelta = kilometers/111; //like allprog said.
span.longitudeDelta = kilometers/(curve * 111);
MKCoordinateRegion region;
region.span = span;
region.center = location.coordinate;
[self.mapView setRegion:region animated:YES];
This way i set mapView to show distance region to 0.5 km.
EDIT:
Whoa, i digging in my old 'liked' question to show you some original answer, but found a better one below accepted one:
how to make mapview zoom to 5 mile radius of current location
Look at #Anurag answer
To get precise value you should try with
minLattitude = currentLattitude - (RadiusInKm/111.12);
maxLattitude = currentLattitude + (RadiusInKm/111.12);
Thus in your case RadiusInKm = 0.5
For finding in & max longitude data you need to follow the same thing but but you have to multiply the result with cosine function of latitude
I would do this way.
double accuracy = 0.1;//How accurate do you want. Smaller value, slower perform
double distance = 500;//Distance you want
Create infinite loop.
In the loop check whether distance is bigger than 500. If yes, break. If not, add 0.1 value to latitude or longitude.
Do above way to get Max longitude, max latitude, min longitude and min latitude.
Compare your DB, if CLLocation is inside of the value, then return.
I cannot say this is the best way to solve your problem. Because we are guessing value...If you know how to convert CLLocation from given distance, that is better!
This should be correct (in php)
https://www.movable-type.co.uk/scripts/latlong-db.html
$R = 6371; // earth's mean radius, km
$rad = 0.5
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
$maxLon = $lon + rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
$minLon = $lon - rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
I have two points whose latitude and longitude i know.
How can i calculate the distance(in Km and Miles) between them. What is the formulae?
You can use the haversine formula to calculate such distances.
Use the haversine Formula for this...
Here is the link having java script code to calculate distance
http://www.movable-type.co.uk/scripts/latlong.html
A = LAT1, B = LONG1
C = LAT2, D = LONG2 (all converted to radians: degree/57.29577951)
IF A = C AND B = D THEN DISTANCE = 0;
ELSE
IF [SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)] > 1 THEN DISTANCE = 3963.1*ARCOS[1];
ELSE
DISTANCE=3963.1*ARCOS[SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)];
For an accurate and complete (works with any pair of points) solution
use my geodesic calculator at
http://geographiclib.sf.net/cgi-bin/GeodSolve. The formulas are given in
http://arxiv.org/abs/1102.1215.