Swift Doble rounding up after 7 decimal places - swift

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.

Related

Finding area of n-sided polygon with array of decimal latitude and longitude

I am trying to find the area of a polygon generated by a users path. The path gives back an array of lat/lon. The path is always self closing. I have tried multiple methods that I found online. Below is what I currently have, I can't make sense of the outputted data. The area * 6378137.0 * 6378137.0 is meant to give me back an area in m^2, but the results are massive numbers.
My ideal solution would be to find a way to (with an assumed % of error) map my lat/lon to xy coordinates and apply the shoelace theorem. I understand that this is pretty hard to do accurately because of the curvature of the earth.
Building from the last paragraph, maybe the best way to map to x,y coordinates would be some sort of projection method. I have not gone too far down that path yet.
What would be the best method to try in order to solve this problem? If someone could set me on the right path or decipher the code I have already tried I would greatly appreciate it (code is done in swift playground). Thanks!
func deg2rad(_ number: Double) -> Double {
return number * .pi / 180
}
func areaCalc(lat: [Double]?, lon: [Double]?){
guard let lat = lat,
let lon = lon
else { return }
var area: Double = 0.0
if(lat.count > 2){
for i in stride(from: 0, to: lat.count - 1, by: 1) {
let p1lon = lon[i]
let p1lat = lat[i]
let p2lon = lon[i+1]
let p2lat = lat[i+1]
area = area + (deg2rad(p2lon - p1lon)) * (2 + sin(deg2rad(p1lat))) + (sin(deg2rad(p2lat)))
}
area = area * 6378137.0 * 6378137.0
area = abs(area / 2)
}
}

Calculate distance between 2 latitude [duplicate]

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

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?

Having trouble putting in the coordinates in global variable [CLgeocoder, swift4]

I am trying to create an app that will find the sunrise time and I am using this web API that requires both coordinates of the location.
This is my solution for to get the latitude and longitude but then it does not let me store the coordinates in the variables.(The coordinates must be float)
Why doesn't it let me put those numbers inside the variables ? or is there a better way of implementing this?
let lattitude: Float = 0.0000
let longitude: Float = 0.0000
let address = "Tokyo"
CLGeocoder().geocodeAddressString(address) { placemarks, error in
if let lat = placemarks?.first?.location?.coordinate.latitude{
print("lattitude : \(lat)")
lattitude = lat
}
if let lng = placemarks?.first?.location?.coordinate.longitude{
print("longtitude : \(lng)")
longtitude = lng
}
}
You have specified that the variables lattitude and longitude as let. These are constants and cannot have their values changed after initialization. Change the let to var.
Also another problem is placemarks?.first?.location?.coordinate.latitude and placemarks?.first?.location?.coordinate.longitude returns type double. You cannot assign a double to a float.
You can either try casting lat/lng to type Float.
lattitude = Float(lat)
longitude = Float(lng)
Or changing the lattitude and longitude variable to type Double.
var lattitude: Double = 0.0000
var longitude: Double = 0.0000