iOS-地图报错超出了经纬度范围Invalid Region

做地图定位的时候,使用一下代码

            // 经纬度
            CLLocationDegrees latitude = [storeDict[@"lat"] doubleValue];
            CLLocationDegrees longitude = [storeDict[@"lon"] doubleValue];
            
            CLLocationCoordinate2D centerCoord = (CLLocationCoordinate2D){latitude, longitude};
            MKCoordinateSpan span;
            span.latitudeDelta=0.01;
            span.longitudeDelta=0.01;
            MKCoordinateRegion region={centerCoord,span};
            [self.mapView setRegion:region animated:true];

报错信息: 

Exception : 'Invalid Region <center:+inf, +0.00000000 span:+1.00000000, +0.50000000>' when trying to display the map

错误问题解释: Invalid Region 的意思是无效的区域.使用的经纬度超出了规定的经纬度范围.跑出的错误.

扫盲一下地理知识(我也是百度的...)

 经度0°——180°(东行,标注E)0°——180°(西行,标注W)
 纬度0°——90°N、0°——90°S

也就是说纬度的范围 -90 <= latitude <= 90   经度的范围是 -180 <= longitude <= 180

找到错误原因:后台把经纬度写反了...本来纬度应该是31.190606错写成经度121.443078.

解决办法: 加上判断

            if ((region.center.latitude >= -90) && (region.center.latitude <= 90) && (region.center.longitude >= -180) && (region.center.longitude <= 180))
            {
                [self.mapView setRegion:region animated:true];
            }
            else
            {
                NSLog(@"invilid region");
            }
原文地址:https://www.cnblogs.com/mancong/p/6117440.html