Calculate distance between 2 latitude [duplicate] - swift

This question already has answers here:
Calculate distance between 2 point on maps for iOS [closed]
(2 answers)
Closed 11 months ago.
i have 2 latitude obtain on this format:
let lat1 = 37.33756323
let lat2 = 37.33683958
now I need to calculate the distance between this 2 point.
converting this to hours, min and second I'm able to calculate the distance .. which result in around 0.08km considering that for 1 deg of lat = 60 NM
But how can I do it with swift.. is there any way to first convert this lat in hours, min and second? I can't find the right way to "subtract" correctly this 2 angles.
any suggestion?
thanks a lot

from the code at: Calculate distance between 2 point on maps for iOS
import CoreLocation
let lat1 = 37.33756323
let lat2 = 37.33683958
let coord0 = CLLocation(latitude: lat1, longitude: 0.0)
let coord1 = CLLocation(latitude: lat2, longitude: 0.0)
let d = coord0.distance(from: coord1)
print("\n----> d = \(d) (m) ") // 80.31355191695503 meters
print("----> d = \(d/1000.0) (km) \n") // 0.08031355191695504 kilometers

Related

How can I convert distance from one location to another in miles swift?

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

Is there a way to convert meters to kilometers in dart?

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?

Swift Doble rounding up after 7 decimal places

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.

Find Minimum/Maximum latitude and Longitude

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

How can i get minimum and maximum latitude and longitude using current location and radius?

I want Minimum and Maximum Latitude and Longitude Using Current Location.Suppose i have give the area of 20km so using that latitude and Longitude i want Maximum latitude and Longitude and Minimum latitude and Longitude also want all latitude and Longitude in between this so how can i get it.How can i use of radius in it.Please help me.Thanks in Advance...
you can manually calculate it... I don't know if any other way exist
1° latitude = 69.047 statute miles = 60 nautical miles
= 111.12 kilometers
so for 20 kilometers it would be around 0.18 latitude
For longitude the conversion is the same as latitude except the value is multiplied by the cosine of the latitude.
To set the same range on map for display
newRegion.center=newLocation.coordinate;
// newRegion.span.latitudeDelta = (20*2)/111.12; // For kilometers
newRegion.span.latitudeDelta = (20*2)/60.0; // For Miles
newRegion.span.longitudeDelta = ((20*2)/60.0) *(cos(newRegion.span.latitudeDelta)); // For Miles
mapView.region = newRegion;
It will set 20 kilometer's range on map that is displayed...
so you can find it by
you can find it by
minLattitude = currentLattitude - (RadiusInKm/111.12);
maxLattitude = currentLattitude + (RadiusInKm/111.12);
For longitude same but multiply the result with cosine of latitude...
1) Macros to convert Degrees to Radians:
#define DEGREES_TO_RADIANS(degrees) (degrees / 180.0 * M_PI)
2) Macros to raduis in KM:
#define radiusInKM 5.00
3) Set the minimum and maximum Latitude, Longitude values
CLLocation *location;//your current location
1° latitude = 69.047 statute miles = 60 nautical miles = 111.12 kilometers
double minLat = location.coordinate.latitude - (radiusInKM/111.12);
double maxLat = location.coordinate.latitude + (radiusInKM/111.12);
double minLon = location.coordinate.longitude - (radiusInKM) / fabs(cos(DEGREES_TO_RADIANS(location.coordinate.longitude))*111.12);
double maxLon = location.coordinate.longitude + (radiusInKM) / fabs(cos(DEGREES_TO_RADIANS(location.coordinate.longitude))*111.12);
macros to convert Degrees to Radians
define DEGREES_TO_RADIANS(degrees) (degrees / 180.0 * M_PI)
macros to raduis in KM
define radiusInKM 5.00
set the minimum and maximum Latitude, Longitude values
CLLocation *location;//your current location
1° latitude = 69.047 statute miles = 60 nautical miles = 111.12 kilometers
double minLat = location.coordinate.latitude - (radiusInKM/111.12);
double maxLat = location.coordinate.latitude + (radiusInKM/111.12);
double minLon = location.coordinate.longitude - (radiusInKM) / fabs(cos(DEGREES_TO_RADIANS(location.coordinate.longitude))*111.12);
double maxLon = location.coordinate.longitude + (radiusInKM) / fabs(cos(DEGREES_TO_RADIANS(location.coordinate.longitude))*111.12);