MKMapView NSInvalidArgumentException ios 9 swift中的区域崩溃无效?

B.S*_*mar 2 swift

我试图在TableView中查看MKMapView,但它已经崩溃了一些问题,但我无法找到问题,请任何人帮我清除这个问题?它显示这样......

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'无效的区域中心:-119.27656620,+ 34.26884010 span:+0.04487346,-0.12825858>'

在这里,我给我的代码我做了什么..

let location = CLLocationCoordinate2D(
        latitude: longarray[indexPath.row] as! Double,
        longitude: latarray[indexPath.row] as! Double

    )
    print("the json array of Longitude is  \(self.longarray[indexPath.row])")
    print("the json array of Longitude is  \(self.latarray[indexPath.row])")

    let span = MKCoordinateSpanMake(2.0, 2.0)
    print("the span is  \(span)")
    let region = MKCoordinateRegion(center: location, span: span)

    cell.mapView.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()
    annotation.coordinate = location

    cell.mapView.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)

Lam*_*our 6

错误是非常直接的:在latitude你传递经度值并且longitude你做了同样的事情,所以切换它们

在你的代码中:

let location = CLLocationCoordinate2D(
        latitude: longarray[indexPath.row] as! Double, //<-- error
        longitude: latarray[indexPath.row] as! Double  //<--- error

    )
Run Code Online (Sandbox Code Playgroud)

纬度属性无法接收任何经度值,因为它们是负数

正确的代码应该是:

let location = CLLocationCoordinate2D(
        latitude: latarray[indexPath.row] as! Double,
        longitude: longarray[indexPath.row] as! Double

    )
Run Code Online (Sandbox Code Playgroud)

  • 创建坐标后,func`CLLocationCoordinate2DIsValid(CLLocationCoordinate2D)`检查位置是否有效@kalpeshjetani (3认同)