如何将CoreLocation坐标转换为String?

时间:2015-04-06 06:25:35

标签: ios string oop swift core-location

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var user_lat = manager.location.coordinate.latitude
    var user_long = manager.location.coordinate.longitude
    self.locationManager.stopUpdatingLocation()
}

我已尝试String(manager.location.coordinate.latitude),但这似乎不起作用。

3 个答案:

答案 0 :(得分:1)

尝试使用格式

let user_lat = String(format: "%f", manager.location.coordinate.latitude)
let user_long = String(format: "%f", manager.location.coordinate.longitude)

答案 1 :(得分:-1)

首先,您必须将其读作double,然后将其转换为字符串。代码应该是这样的。检查语法,我还没有编译代码。

let lat: Double = manager.location.coordinate.latitude
let user_lat : String = String(format:"%f", lat)

答案 2 :(得分:-1)

你可以尝试这个(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)