问题
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var user_lat = manager.location.coordinate.latitude
var user_long = manager.location.coordinate.longitude
self.locationManager.stopUpdatingLocation()
}
I've tried String(manager.location.coordinate.latitude) but that doesn't seem to work.
回答1:
Try with format
let user_lat = String(format: "%f", manager.location.coordinate.latitude)
let user_long = String(format: "%f", manager.location.coordinate.longitude)
回答2:
Firstly, you must read it as a double and then convert it into string. Code should be something like this. Check the syntax, I haven't compiled the code.
let lat: Double = manager.location.coordinate.latitude
let user_lat : String = String(format:"%f", lat)
回答3:
you can try this (Swift 3)
guard let newLocation = locations.last else { return }
let latitude = Double(newLocation.coordinate.latitude)
let longitude = Double(newLocation.coordinate.longitude)
let latString = (latitude < 0) ? "S" : "N"
let lonString = (longitude < 0) ? "W" : "E"
let coords = "\(latString) \(latitude) \(lonString) \(longitude)"
print(coords)
来源:https://stackoverflow.com/questions/29466405/how-do-i-convert-corelocation-coordinates-to-a-string