Map 地图
添加
#import <MapKit/MapKit.h>头文件
倒入
mapkit.framework 库
mapkit.framework是属于ui,可以在故事版上 添加 mkmapview
#import "ViewController.h" #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController ()<MKMapViewDelegate> //监控 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