一、导入MapAnnotations类。
在工程的build phases中的link binary with libraryies中点击+号添加MapAnnotations类。
二、头文件中加载类以及协议
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "MapAnnotations.h"
@interface panyongViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>
- (IBAction)getphoto:(id)sender;
@property (nonatomic,strong)IBOutlet MKMapView *map;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (nonatomic,strong)IBOutlet CLLocationManager *locManager;
@property(nonatomic) CLLocationCoordinate2D loc;
@property(nonatomic,strong) IBOutlet MapAnnotations *mapAnnotations;
-(void)setCurrentLocation:(CLLocation *)location;
- (IBAction)segmentChange:(id)sender;
@end
三、定位具体实现
//在viewdidload中添加map及locmanager的初始化
map=[[MKMapView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 410.0f)];
map.showsUserLocation=YES;
[self.view addSubview:map];
locManager=[[CLLocationManager alloc]init];
locManager.delegate=self;
locManager.desiredAccuracy=kCLLocationAccuracyBest;
locManager.distanceFilter=10.0f;
[locManager startUpdatingHeading];
mapAnnotations=[[MapAnnotations alloc]initWithCoordinate:loc];
mapAnnotations.title=@"test";
mapAnnotations.subtitle=@"have a try";
[map addAnnotation:mapAnnotations];
//三种地图格式的转换
- (IBAction)segmentChange:(id)sender {
if (segmentControl.selectedSegmentIndex==0) {
map.mapType=MKMapTypeStandard;
}
if (segmentControl.selectedSegmentIndex==1) {
map.mapType=MKMapTypeHybrid;
}
if (segmentControl.selectedSegmentIndex==2) {
map.mapType=MKMapTypeSatellite;
}
}
#pragma mark-
#pragma mark Core Location Delegate Methods
//地图定位的实现
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
loc=[newLocation coordinate];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.1;
span.longitudeDelta=0.1;
region.span=span;
region.center=loc;
map.mapType=MKMapTypeStandard;
[map setRegion:region animated:YES];
[map regionThatFits:region];
UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
UILabel *label2=[[UILabel alloc]initWithFrame:CGRectMake(0, 15, 10, 10)];
label1.text=[[NSString alloc]initWithFormat:@"%f",span.latitudeDelta];
label2.text=[[NSString alloc]initWithFormat:@"%f",span.longitudeDelta];
[map addSubview: label1];
[map addSubview: label2];
}
//用户拒绝使用地图服务
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSString *errorMessage;
if ([error code]==kCLErrorDenied) {
errorMessage=@"被拒绝访问";
}
if ([error code]==kCLErrorLocationUnknown) {
errorMessage=@"";
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:errorMessage delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
[alert show];
}
}
//更新用户定位地址
-(void)setCurrentLocation:(CLLocation *)location{
MKCoordinateRegion region;
region.center=location.coordinate;
region.span.longitudeDelta=0.15f;
region.span.latitudeDelta=0.15f;
[map setRegion:region animated:YES];
}