【发布时间】:2015-09-05 04:02:12
【问题描述】:
我正在尝试实现一个基本的地图视图并将用户的当前位置作为注释添加到地图中。我已将 requestwheninuse 键添加到我的 info.plist 并导入了 coreLocation。
在我的视图控制器的加载方法中,我有以下内容:
locManager.requestWhenInUseAuthorization()
var currentLocation : CLLocation
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){
currentLocation = locManager.location
println("currentLocation is \(currentLocation)")
}
else{
println("not getting location")
// a default pin
}
我正在收到提示。检索位置的权限。发生这种情况时,我的打印显示没有获取位置,显然是因为它在用户有机会点击 OK 之前运行。如果我启动应用程序并返回,我可以检索位置并将其添加到地图中。但是,我希望当用户第一次点击 OK 时能够抓住当前位置并将其添加到地图中。我怎样才能做到这一点?我有以下添加图钉的方法:
func addPin(location2D: CLLocationCoordinate2D){
self.mapView.delegate = self
var newPoint = MKPointAnnotation()
newPoint.coordinate = location2D
self.mapView.addAnnotation(newPoint)
}
【问题讨论】:
标签: ios swift location mkmapview core-location