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

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

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

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?

Converting MKMapRect.origin to longitude and latitude

I have a need to annotate a visible point on a map(without user input). I am using MKMapView visibleMapRect to calculate the point I want. However from my codes below, I am reading values like
let rect = mapView.visibleMapRect
print(rect.origin.x) //211653840.83766
print(rect.origin.y) //133214809.161136
which are not longitude and latitude. But any example I found of conversion between MKMapRect and Coordinates does it like as though the origin is already given in longitude and latitude. Is there something I need to do to get longitude and latitude from visibleMapRect? Or is there a formula to convert those values I am getting in longitude and latitude?
For additional information the longitude and latitude of a visible point is:
103.861463 //longitude
1.3165999 //latitude
when the visibleMapRect is:
211653840.83766 //origin.x
133214809.161136 //origin.y
16865.6084111989 //size.width
26357.4158187151 //size.height
let rect = mapView.visibleMapRect
let mapPoint = MKMapPointMake(rect.origin.x, rect.origin.y)
let coordinate = MKCoordinateForMapPoint(mapPoint)
print(coordinate.latitude)
print(coordinate.longitude)

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.