【发布时间】:2014-08-03 13:14:34
【问题描述】:
我正在尝试获取用户坐标,但我不知道如何检索它们?这是我在地图上查找他们位置的代码,我应该添加什么来获取经度/纬度的数值?它工作正常,我已将 requestAuthorization 密钥添加到 info.plist。我尝试使用 CLLocation 对象,但我似乎无法让它工作。
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var Map: MKMapView!
var locationMgr = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationMgr.delegate = self
locationMgr = CLLocationManager()
locationMgr.requestAlwaysAuthorization()
locationMgr.startUpdatingLocation()
Map.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
【问题讨论】:
-
您必须实现此方法才能实际检索位置:
- func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) -
感谢您的快速回复!你有没有机会给我一些示例代码,它将位置打印到控制台? func 中的 println(locations) 似乎不起作用,我认为它不断返回 nil 值?我真的很陌生,这是我编码的第一周!
-
谢谢!有用的链接 :) 我现在已经可以使用它了,并且也可以从视图控制器中使用
标签: ios swift coordinates core-location xcode6