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