How do I convert CoreLocation coordinates to a String?

我与影子孤独终老i 提交于 2019-12-11 03:06:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!