如何获取用户的纬度和经度';用户在iOS中的位置
我是Swift的新手,我需要获得用户的当前位置。我的意思是我需要得到纬度和经度。我试过这个:如何获取用户的纬度和经度';用户在iOS中的位置,ios,swift,cllocationmanager,cllocation,Ios,Swift,Cllocationmanager,Cllocation,我是Swift的新手,我需要获得用户的当前位置。我的意思是我需要得到纬度和经度。我试过这个: class ViewController: UIViewController, CLLocationManagerDelegate{ let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() self.locationMana
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)
}
}
这里我可以得到坐标,但它看起来像:
莫斯科标准时间2015年2月14日上午10:48:14,航速+/-100.00米(航速-1.00英里/航程-1.00)
如何仅检索纬度和经度?您可以期待多次调用
didUpdateLocations
,并且随着时间的推移,精确度会提高(假设您所在的地方GPS接收效果良好-室外,而不是高楼环绕)。您可以直接从locations
数组中的CLLocation对象访问纬度和经度
let location = locations[locations.count-1] as CLLocation;
println("\(location.latitude) \(location.longitude)");
func locationManager(管理器:CLLocationManager!,didUpdateLocations位置:[AnyObject]!)
您的位置:[AnyObject]
实际上是一个[CLLocation]
只需获取其最后一个对象并使用CLLocation的坐标
属性
Swift 3.x和Swift 4.x的代码已更新 正如我所见,您在代码中使用了
print(placemark.location)
所以,如果你只想获得纬度,请使用此代码
print(placemark.location.coordinate.latitude)
如果您只想获得经度,请使用此代码
print(placemark.location.coordinate.longitude)
希望这有帮助 嗯,现在我需要知道街道的名称和最近建筑物的编号。我不知道用MapKit怎么做,我用的是谷歌地图sdk。问题是,您必须获得所需位置的地址(可能是CLPlaceMark?)并提取这些数据,或者甚至为您提取这些数据。