在应用开发中,经常会使用到显示地图的功能,地位功能,在ios中 使用MKMapVIew类显示地图, 使用MKMapView 前必须先导入MapKit.framework.
点击项目target->summary 添加 Mapkit.framework.
在ViewController.h头文件中导入 #import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController
@property (nonatomic,strong) MKMapView * myMapView;
@end
在ViewController.m 文件的viewDidLoad 方法中添加如下代码
#import "ViewController.h"
#import "MyAnnotation.h"
@interface ViewController () <MKMapViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.myMapView.mapType = MKMapTypeHybrid;
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.myMapView.delegate = self;
//显示当前位置
self.myMapView.showsUserLocation = YES;
[self.view addSubview:self.myMapView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
@end
MKMapView 地图类型mapType 属性 有三种类型:
enum {
MKMapTypeStandard = 0,
MKMapTypeSatellite,
MKMapTypeHybrid
};
typedef NSUInteger MKMapType;