添加
#import
倒入
mapkit.framework 库
mapkit.framework是属于ui,可以在故事版上 添加 mkmapview
#import "ViewController.h"
#import
#import
@interface ViewController ()
//监控 MKmapview
@property (weak, nonatomic) IBOutlet MKMapView *map;
//为了ios 8 中能 弹出 要访问隐私的对话框
@property(strong,nonatomic) CLLocationManager *mar;
//反地理编码
@property(strong,nonatomic)CLGeocoder *geocoder;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];/*MKMapTypeStandard ,标准地图显示MKMapTypeSatellite,卫星图MKMapTypeHybrid 卫星+普通*/self.map.mapType=MKMapTypeStandard;if ( [[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {// CLLocationManager *mar=[[[CLLocationManager alloc]init];
[self.mar requestAlwaysAuthorization];//手动象用户提示 要访问隐私self.map.userTrackingMode=MKUserTrackingModeFollow;NSLog(@"ios8");}else{NSLog(@"其他");//成为mapview代理 由于ios7中不会自动找到你自己的位置
}self.map.delegate=self;//设置地图不让旋转self.map.rotateEnabled=NO;}
-(CLLocationManager *)mar
{if (!_mar) {_mar=[[CLLocationManager alloc]init];}return _mar;
}
/*每次更新到新用户的位置就会调用MKMapView 促发事件的控件MKUserLocation 大头针模型*/
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{//地图上的蓝色点称为大头针,点击大头针能显示位置
// userLocation.title=@"nn";
// userLocation.subtitle=@"2222";[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {CLPlacemark *placemark=[placemarks firstObject];userLocation.title=placemark.name;userLocation.subtitle=placemark.locality;}];//移动地图到用户所在的位置
[self.map setCenterCoordinate:userLocation.location.coordinate animated:YES];//设置地图显示的区域CLLocationCoordinate2D center=userLocation.location.coordinate;//指定经纬度的跨度MKCoordinateSpan spen=MKCoordinateSpanMake(5, 5);MKCoordinateRegion region=MKCoordinateRegionMake(center, spen);[self.map setRegion:region animated:YES];}-(CLGeocoder *)geocoder
{if (!_geocoder) {_geocoder=[[CLGeocoder alloc]init];}return _geocoder;
}@end