这是我第一次在这里发帖。我目前陷入困境,试图找出如何在我的地图上添加一个按钮,如果他们在地图上偏离它,将重新显示用户的当前位置。目前我有下面写的代码显示用户的当前位置。
!function(t){"function"==typeof define&&false?define
}
我不确定的是,@IBAction func中的代码是什么,如果他们在寻找其他地方时偏离它,将允许地图重新定位到用户的当前位置。
非常感谢任何帮助。
非常感谢!
答案 0 :(得分:5)
为此,您可以在startUpdatingLocation
操作中再次调用CLLocationManager
Button
方法。
要获取用户的正确当前位置,您需要使用last
方法中的location
数组访问didUpdateLocations
对象。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Access the last object from locations to get perfect current location
if let location = locations.last {
let span = MKCoordinateSpanMake(0.00775, 0.00775)
let myLocation = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
let region = MKCoordinateRegionMake(myLocation, span)
Map.setRegion(region, animated: true)
}
self.Map.showsUserLocation = true
manager.stopUpdatingLocation()
}
现在只需在按钮操作上调用startUpdatingLocation
即可。
@IBAction func refLocation(_ sender: Any) {
manager.startUpdatingLocation()
}