库
coreMotion.framework
CoreLocation.framework
MapKit.framework
第一个页面
#import
#import< CoreLocation/CoreLocation.h>
{
CLLocationManager *manager;
}
@property(nonatomic,strong)MKMapView *mapView;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 实例化地图视图
self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
// 设置地图类型
self.mapView.mapType = MKMapTypeHybrid; // 混合地图
self.mapView.mapType = MKMapTypeSatellite; // 卫星图
self.mapView.mapType = MKMapTypeStandard; // 平面地图
// 设置地图可以滚动
self.mapView.scrollEnabled = YES;
// 设置可以啮捏合
self.mapView.pitchEnabled = YES;
// 设置可以旋转
self.mapView.rotateEnabled = YES;
// 设置可以点击缩放
self.mapView.zoomEnabled = YES;
// 设置委托
self.mapView.delegate = self;
// 设置可以显示用户当前位置
self.mapView.showsUserLocation = YES;
//
[self.view addSubview:self.mapView];
// 盗用CLLocationManager获取当前位置
manager = [[CLLocationManager alloc] init];
//距离过滤
manager.distanceFilter = 10;
manager.desiredAccuracy = kCLLocationAccuracyBest;
manager.delegate = self;
manager.pausesLocationUpdatesAutomatically = YES;
[manager setActivityType:CLActivityTypeOther];
[manager requestAlwaysAuthorization];
// 添加“长按手势”,触发后可以显示当前位置,
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showCurrentLocation:)];
[self.mapView addGestureRecognizer:longPress];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getPointToCoordinate:)];
[self.mapView addGestureRecognizer:tap];
-(void)getPointToCoordinate:(UITapGestureRecognizer *)reco
{
// 北京八维研修学院
// 获得手势点击的坐标
CGPoint point = [reco locationInView:self.mapView];
// 把坐标点转换为经纬度信息
CLLocationCoordinate2D coor = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
NSLog(@"纬度:%g,经度:%g",coor.latitude,coor.longitude);
// 添加锚点
MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc] init];
pointAnno.coordinate = coor;
pointAnno.title = [NSString stringWithFormat:@"纬度:%g,经度:%g",coor.latitude,coor.longitude];
// 根据经纬度解析为地址字符串()
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks lastObject];
pointAnno.subtitle = placeMark.name;
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapView addAnnotation:pointAnno];
});
}];
}
//
-(void)showCurrentLocation:(UILongPressGestureRecognizer *)reco
{
[manager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
// 设置显示范围,参数值越小越精确
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
// 设置显示区域(包括中心点和范围)
MKCoordinateRegion region = MKCoordinateRegionMake(currentLocation.coordinate, span);
// 把表示区域的圆形区域转换为一个符合地理方圆的区域
MKCoordinateRegion fitRegion = [self.mapView regionThatFits:region];
// 让地图显示当前位置
[self.mapView setRegion:fitRegion animated:YES];
}
#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
NSLog(@"显示区域将要变化");
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"显示区域变化完成");
}
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
NSLog(@"将要加载地图");
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"加载地图完成");
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"加载地图失败");
}
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView NS_AVAILABLE(10_9, 7_0)
{
NSLog(@"将要渲染地图");
}
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered NS_AVAILABLE(10_9, 7_0)
{
NSLog(@"渲染地图完成");
}