iOS8.0以上出错Terminating app due to uncaught exception \'NSInvalidArgumentException\', reason: \'Invalid Region\'

   项目中需要加入导航系统,所以使用系统的MKMapView去显示地图,之前我用的测试机是7.0的,一切显示ok,但是一到8.0以上马上崩掉,心里很纳闷,不由去埋怨iOS的兼容机制(肯定有猿友会说你自己写的不好,还埋怨别人,那我只能呵呵了)。不管怎样,bug还是要改的,下面贴出我写的部分代码和我的解决方法, MapViewController的关键代码(代码红色注释部分就是代码崩溃的位置和信息):

#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

#pragma mark MapViewController.h

@interface MapViewController : UIViewController<MKMapViewDelegate>

{

    MKMapView *naviMapView;

}


- (void)openLocation;


#pragma mark MapViewController.m

#import "MapViewController.h"


- (void)viewDidLoad

{

    [super viewDidLoad];


    naviMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];

    naviMapView.delegate = self;

    naviMapView.mapType = MKMapTypeStandard;

    [self.view addSubview:naviMapView];

    [naviMapView release];

}


- (void)openLocation

{

    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(31.082437934096,121.43184477272);

    MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(coords,40000 ,40000 );

    MKCoordinateRegion adjustedRegion = [naviMapView regionThatFits:region];

    [naviMapView setRegion:adjustedRegion animated:YES];// iOS8.0以上这里报错,显示Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region'

}


ViewController调用的关键代码:


#import <UIKit/UIKit.h>

#import "MapViewController.h"

@interface ViewController : UIViewController

{

    MapViewController* mapViewController;

}


#pragma mark ViewController.m


- (void)dealloc

{

    if (mapViewController)

    {

        [mapViewController release];

        mapViewController = nil;

    }

    [super dealloc];

}


- (void)viewDidLoad

{

    [super viewDidLoad];


    mapViewController = [[MapViewController alloc] init];

    mapViewController.view.frame = CGRectMake(310, 60, 704, 698);

    [self.view addSubview:mapViewController.view];


// 打开地图

UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 40, 40)];

    [btn addTarget:self action:@selector(openMap) forControlEvents:UIControlEventTouchUpInside];

    [btn setTitle:@"打开地图" forState:UIControlStateNormal];

    [self.view addSubview:btn];

    [btn release];

}


- (void)openMap

{

    [mapViewController openLocation];

}


     开始用注释大法把regionThatFits注释掉然后就没有报错,以为是转换之后的经纬度不合法,一直从这个出发点入手,网上也搜了相关的资料,但是无法解决我的问题。  最后实在没有办法,我就转换了一下思路,从地图demo入手,然后从网上下载了一个 MapDemo ,并且感谢荣芳志的分享!
     这里分享了找bug的思路,希望大家不要说我啰嗦,好了这里说下比较后的结果是MPMapView不能从属于UIView的子类,或者像我一样调用。这两种我都测试了,都会闪退!下面贴出修改后的代码,修改后的ViewController关键代码如下图所示:
   

#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate>

{

    MKMapView* naviMapView;

}


#pragma mark ViewController.m

- (void)viewDidLoad

{

    [super viewDidLoad];


    naviMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];

    naviMapView.delegate = self;

    naviMapView.mapType = MKMapTypeStandard;

    [self.view addSubview:naviMapView];

    [naviMapView release];


 // 打开地图

    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 40, 40)];

    [btn addTarget:self action:@selector(openLocation) forControlEvents:UIControlEventTouchUpInside];

    [btn setTitle:@"打开地图" forState:UIControlStateNormal];

    [self.view addSubview:btn];

    [btn release];

}


- (void)openLocation

{

    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(31.082437934096,121.43184477272);


    MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(coords,40000 ,40000 );


    MKCoordinateRegion adjustedRegion = [naviMapView regionThatFits:region];


    [naviMapView setRegion:adjustedRegion animated:YES]; // 成功修复


}

好了,今天就介绍到这里了,有什么问题可以给我留言,如果有更好的想法,更可以留言,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值