需要某些坐标才能在中心MKCoordinateRegion setRegion的Mapview上显示

时间:2019-04-04 22:29:25

标签: swift mapkit

我从另一个来源获取坐标,我想将这些坐标显示为地图的中心。但是,它总是崩溃。如何使其有效?我将其转换为double,并在stackoverflow上尝试了其他解决方案,但它们似乎已过时。

*** 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)

1 个答案:

答案 0 :(得分:0)

尝试使用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)
}