地理位置信息
启用CoreLocation
1.在Info.plist
中写明对设备的要求
2.在Info.plist
中选择提示用户授权的信息
可以设定When
或者Always
3.在viewDidLoad
中添加
self.locationManager = [CLLocationManager new];
[self.locationManager requestWhenInUseAuthorization];
//or
//[self.locationManager requestAlwaysInUseAuthorization];
4.确认是否获得用户允许
[CLLocationManager authorizationStatus];
5.每次启动时可以先判断用户是否曾经允许过
[CLLocationManager locationServicesEnabled];
使用CoreLocation(地址信息)
//delegate:
@interface ViewController () <CLLocationManagerDelegate, MKMapViewDelegate>
@property (strong, nonatomic) CLLocationManager * locationManager;
if ( !self.locationManager ){
self.locationManager = [CLLocationManager new];
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;//精度
self.locationManager.distanceFilter = kCLDistanceFilterNone; // 任何
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
//精确
[self.locationManager startUpdatingLocation];
//500米以上变化,每5分钟不超过一次通知
//[self.locationManager startMonitoringSignificantLocationChanges];
//区域模式
//[self.locationManage startMonitoringForRegion:<#(nonnull CLRegion *)#>];
//Visit 模式(略)
位置变动后的处理
//位置变动后的处理
-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
CLLocation * location = locations[0];//当前位置
self.locationLabel.text = [NSString stringWithFormat:@"Location: {%lf , %lf} ^%lf) Direction=%lf degree, Floor=%d",
location.coordinate.longitude,//经度
location.coordinate.latitude,//纬度
location.altitude,//海拔
location.course,//方向
(int)location.floor.level//楼层
];
}
地图显示
实现
1.打开功能(TARGETS)
2.把MKMapView放到界面
地图坐标系理论知识
iOS里有3个坐标系
地理坐标
Mercator投影坐标
-
UIView的视图坐标系
//经纬度坐标系和Mercator投影坐标系换算 MKMapPointForCoordinate(<#CLLocationCoordinate2D coordinate#>) MKCoordinateForMapPoint(<#MKMapPoint mapPoint#>)
MKMapView类型
MKMapView 用法
-
基本用法同一般的UIView
-
配置:显示区域
//经纬度 .region:显示区域 .centerCoordinate:不改变区域大小 //Mercator地图坐标 .visibleMapRect,edgePaddings
-
显示用户位置
.showsUserLocation
委托.delegate
-
MKMapView 标注
-
MKPoinAnnotation
.coordinate//放哪里 .title/.subtitle//默认选中信息 [MKMapView addAnnotation:];//加载
MKAnnotationView:怎么展示
content:image or subclass , .canShowCallout
mapView.delegate供应:mapView:viewForAnnotation:
标准视图 MKPinAnnotationView
通知: mapView:didAddAnnotationView
MKMapView -更多
self.locationManager = [CLLocationManager new];
[self.locationManager requestWhenInUseAuthorization];
self.mapView.delegate = self;
self.mapView.mapType = MKMapTypeHybridFlyover;
self.mapView.showsUserLocation = YES;
//self.mapView.visibleMapRect = MKMapRectMake(0, 0, 200000, 200000);
self.mapView.region = MKCoordinateRegionMake(self.mapView.centerCoordinate, MKCoordinateSpanMake(1, 1));
if (!self.mapView.userLocationVisible) {
self.mapView.centerCoordinate = self.mapView.userLocation.coordinate;//移动地图位置
MKCircle * circle = [MKCircle circleWithCenterCoordinate:self.mapView.centerCoordinate radius:1000];
[self.mapView addOverlay:circle];
}
MKCircle * circle = [MKCircle circleWithCenterCoordinate:location.coordinate radius:200];
[self.mapView addOverlay:circle];
[UIView animateWithDuration:1.0 animations:^{
self.mapView.centerCoordinate = location.coordinate;
}];
MKPointAnnotation * point = [[MKPointAnnotation alloc] init];
point.coordinate = location.coordinate;
point.title = @"You are here!";
[self.mapView addAnnotation:point];
[self.locationManager stopUpdatingLocation];