#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'
}
开始用注释大法把regionThatFits注释掉然后就没有报错,以为是转换之后的经纬度不合法,一直从这个出发点入手,网上也搜了相关的资料,但是无法解决我的问题。 最后实在没有办法,我就转换了一下思路,从地图demo入手,然后从网上下载了一个 MapDemo ,并且感谢荣芳志的分享!
#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];
}
#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]; // 成功修复
}
好了,今天就介绍到这里了,有什么问题可以给我留言,如果有更好的想法,更可以留言,谢谢!