![](/img/trans.png)
[英]xcode - How to find your current location without CoreLocation
[英]How to Add an annotation At your current location using CoreLocation
除了实际的按钮功能代码之外,我所有的工作都可以进行。 这是我的代码,但未添加注释。
@IBAction func AddAnnotation(_ sender: UIButton) {
CLLocationManager().startUpdatingLocation()
let annotation = MKPointAnnotation()
// Set the annotation by the lat and long variables
annotation.coordinate = CLLocationCoordinate2D()
annotation.title = "My Car"
self.map.addAnnotation(annotation)
}
您的代码可能无法正常工作。 当您调用startUpdatingLocation()
,GPS会花费几秒钟来启动并获得准确的定位。 您必须将自己设置为委托,然后等待您的locationManager(_:didUpdateLocations:)
方法被调用。
您需要将代码放入该方法中添加注释。
正如其他人所说,您还需要编写代码以检查您是否被授权获取用户的位置,如果没有,则请求权限。 (并且,您需要在应用的info.plist文件中添加一个特殊键,以指示您的应用需要访问用户的位置。)
查看有关Core Location的文档,并在Xcode中使用location Manager,您可能想要找到一个指导您完成这些步骤的教程。 这有点涉及。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.