我是一名Swift新手,需要获取用户的当前位置,也就是经纬度。我尝试了以下代码:
class ViewController: UIViewController, CLLocationManagerDelegate{
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
println("ERROR:" + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.displayLocationInfo(pm)
} else {
println("Error with data")
}
})
}
func displayLocationInfo(placemark: CLPlacemark) {
// self.locationManager.stopUpdatingLocation()
println(placemark.locality)
println(placemark.postalCode)
println(placemark.administrativeArea)
println(placemark.country)
println(placemark.location)
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
println("Error:" + error.localizedDescription)
}
}
我可以获取到坐标,但是它看起来像这样:
<+55.75590390,+37.61744720> +/- 100.00m (速度-1.00 mps / 路线-1.00) @ 2/14/15, 10:48:14 AM 莫斯科标准时间
我怎样才能只获取纬度和经度呢?