我试着从VC1到VC2,用按钮1通过segue进入地图,它必须把针聚焦在地图中心的我的位置上,而且它有个错误,它不聚焦在用户身上,但如果我按按钮2,回去再按按钮1,它就会聚焦在用户身上。我不明白我需要在哪里解决这个问题。
最后一个bug:如果我设置了很大的regionInMeters值,比如10000m,然后我放大了,当我试图对用户使用这个方法时,它在两种情况下都会显示地名,街道和bug(图像)。如果我设置低到500,在这些情况下,它显示正确的接近,但只是用户周围的位置,如果我切换到其他城市并放大,它仍然会有这个错误。同样的故事与焦点的地方。在巨大的高度--国名、城市、河流都是正确的。.
let annotationIdentifire = "annotationIdentifire"
let locationManager = CLLocationManager()
let regionInMeters = 500.00
var incomeSegueIdentifire = ""
override func viewDidLoad() {
super.viewDidLoad()
addressLabel.text = ""
mapView.delegate = self
setupMapView()
checkLocationServices()
}
private func checkLocationServices(){
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuthorization()
} else{...}
private func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
if incomeSegueIdentifire == "getAddress" { showUserLocation() }
break
//other cases
}
另外,我在VC1上有另一个按钮2,它用地图打开VC2,它关注的是地方,有我设置的地址,在这种情况下,我在地图上有按钮3,它关注的是我自己。
@IBAction func centerViewInUserLocation() {
showUserLocation()
}
有一种方法可以让我把注意力集中在用户身上。
private func showUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion(center: location,
latitudinalMeters: regionInMeters,
longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
}
我在另一个类中设置了incomeSegueIdentifire的值。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let identifire = segue.identifier, let mapVC = segue.destination as? MapViewController else { return }
mapVC.incomeSegueIdentifire = identifire
}
我没有找到如何修复地名的错误,但我修复了主要的错误(当通过分道打开地图时,它必须关注用户,但它不是)。
我得到的错误是:无法检索区域信息。我试图找到这个问题的答案,但没有找到。找到了几个关于这个错误的答案,那里说,这个代码是正确的,但仍然是错误的,主要的答案建议 - 尝试在你的真实设备上运行它,可能是正确的,我没有iphoneipad。但对于模拟器,我通过异步调用这个方法,用延迟来解决,对我来说是可行的)
private func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
if incomeSegueIdentifire == "getAddress" { DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.showUserLocation()
}
}
//other cases
}
}
但是,如果你对这些问题有什么想法,我还是很高兴知道))。)