我试图在加载视图后将 map zoom 到用户的当前位置,但在加载视图时会出现错误"*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region '".有人能帮忙吗?

干杯

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKCoordinateRegion mapRegion;   
    mapRegion.center.latitude = map.userLocation.coordinate.latitude;
    mapRegion.center.longitude = map.userLocation.coordinate.longitude;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;
    [map setRegion:mapRegion animated: YES];   
}

推荐答案

你定了showsUserLocation = YES吗?如果设置为NOMKMapView将不会更新位置.所以要确保这一点.

MKMapView对象很可能还没有用户位置.要做到这一点,您应该采用MKMapViewDelegate协议并实现mapView:didUpdateUserLocation:

map.delegate = self;

...
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{
    MKCoordinateRegion mapRegion;   
    mapRegion.center = mapView.userLocation.coordinate;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;

    [mapView setRegion:mapRegion animated: YES];
}

Objective-c相关问答推荐

为何在Objective-C中,使用self.propName赋值会导致崩溃而使用_ivarName则不会?

如何以编程方式获取iphone的IP地址

Objective c 判断文本字段是否为空

TWTweetComposeViewController 在 IOS6 中已弃用

如何获取目标名称?

具有动态高度的子视图的自动布局 UIScrollView

为什么 NSDateFormatter 为这 4 个时区返回 nil 日期?

有没有办法清除 UIImage 类使用的缓存?

如何更改图像 tintColor

`-fembed-bitcode` 和 BITCODE_GENERATION_MODE 有什么区别?

iOS - UITableViewCell 中 UIButton 的延迟Touch Down事件

CF 对象与 NS 对象

接口类型不能静态分配?

WKWebView 判断 JavaScript 返回值

NSMutableArray 按顺序添加对象

如何逐行从 NSFileHandle 读取数据?

我可以在 Objective-C switch 语句中声明变量吗?

创建 NSTextField标签的示例代码?

writeToFile:atomically: atomically 是什么意思?

更改 UITextField 占位符字体