1、创建一个 MKMapView 的类,然后把他以一个子视图的形式添加到你的控制器文件中。 如下是一个视图控制器创建了一个 MKMapView 并全屏显示的.h 文件例子。
#import
#import
@interface Creating_a_Map_ViewViewController : UIViewController
@property (nonatomic, strong) MKMapView *myMapView;
@end
这个是一个有 MKMapkit 类型变量的根视图控制器的例子,然后在视图控制器的实现文件(.m 文件),我们将初始化地图并设置它的类型为卫星模式,如下。
@implementation Creating_a_Map_ViewViewController
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.myMapView.mapType = MKMapTypeSatellite;
UIViewAutoresizingFlexib
[self.view addSubview:self.myMapView];
}
我们可以通过 MKMapView 的 mapType 属性来切换地图的展现形式。有如下的属性值可 用:
MKMapTypeStandard
用这个类型来显示普通地图(这个是默认的)。
MKMapTypeSatellite
用这个来类型来显示卫星云图
MKMapTypeHybrid
用这个类型来显示普通地图覆盖于卫星云图之上,这个地图的展现形式属于复合形式。
2、处理 Map 视图上的事件
将遵循 MKMapViewDelegate 协议的 delegate 对象赋值给 MKMapView 实例对象的 delegate 属性。
self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.myMapView.mapType = MKMapTypeSatellite;
self.myMapView.delegate = self;
self.myMapView.autoresizingMask = UIViewAutoresizingFlexib
[self.view addSubview:self.myMapView];
把上面这段代码添加到一个视图控制器的 viewDidLoad 方法里面,很容易就能运行。 不过前提是你需要在头文件中添加类似如下的内容
#import
#import
@interface Handling_the_Events_of_a_Map_ViewViewController : UIViewController <</span>MKMapViewDelegate>
@property (nonatomic, strong) MKMapView *myMapView; @end
3、精确定位设备的位置
if ([CLLocationManager locationServicesEnabled])
{
self.myLocationManager = [[CLLocationManager alloc] init];
self.myLocationManager.delegate = self;
self.myLocationManager.purpose =
@"To provide functionality based on user's current location.";
[self.myLocationManager startUpdatingLocation];
} else {
NSLog(@"Location services are not enabled");
}
#import
@interface Pinpointing_the_Location_of_a_DeviceViewController
: UIViewController <</span>CLLocationManagerDelegat
@property (nonatomic, strong) CLLocationManager *myLocationManager;
@end
View controller 的实现代码如下:
#import "Pinpointing_the_Location_of_a_DeviceViewController.h"
@implementation Pinpointing_the_Location_of_a_DeviceViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocationfromLocation:(CLLocation *)oldLocation{
NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
}
- (void)viewDidLoad {
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled])
{ self.myLocationManager = [[CLLocationManager alloc] init];
self.myLocationManager.delegate = self;
self.myLocationManager.purpose =
@"To provide functionality based on user's current location.";
[self.myLocationManager startUpdatingLocation];
} else {
NSLog(@"Location services are not enabled");
}
}
- (BOOL)shouldAutorotateToInterf
return YES;
}
@end