需要某些坐标显示在中心 MKCoordinateRegion setRegion 的 Mapview 上
Need certain coordinates to show on Mapview on center MKCoordinateRegion setRegion
我从其他来源获取坐标,我想将这些坐标显示为我的地图的中心。但是,它总是崩溃。我如何使它有效?我将它转换为 double,在 Whosebug 上尝试了其他解决方案,但它们似乎已经过时了。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:-121.90043000, +37.32721300 span:+0.02000000, +0.02000000>'
let viewRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: station.location.coordinate.latitude, longitude: station.location.coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02))
mapView.setRegion(viewRegion, animated: true)
尝试使用 MKCoordinateRegionMakeWithDistance
我使用下面的代码来处理与您类似的情况
func centerMapOnUserLocation() {
guard let coordinate = locationManager.location?.coordinate else { return }
let coordinatRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinatRegion, animated: true)
}
我从其他来源获取坐标,我想将这些坐标显示为我的地图的中心。但是,它总是崩溃。我如何使它有效?我将它转换为 double,在 Whosebug 上尝试了其他解决方案,但它们似乎已经过时了。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:-121.90043000, +37.32721300 span:+0.02000000, +0.02000000>'
let viewRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: station.location.coordinate.latitude, longitude: station.location.coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02))
mapView.setRegion(viewRegion, animated: true)
尝试使用 MKCoordinateRegionMakeWithDistance
我使用下面的代码来处理与您类似的情况
func centerMapOnUserLocation() {
guard let coordinate = locationManager.location?.coordinate else { return }
let coordinatRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinatRegion, animated: true)
}