地理位置信息
啟用CoreLocation
1.在Info.plist
中寫(xiě)明對(duì)設(shè)備的要求
2.在Info.plist
中選擇提示用戶授權(quán)的信息
可以設(shè)定When
或者Always
3.在viewDidLoad
中添加
self.locationManager = [CLLocationManager new];
[self.locationManager requestWhenInUseAuthorization];
//or
//[self.locationManager requestAlwaysInUseAuthorization];
4.確認(rèn)是否獲得用戶允許
[CLLocationManager authorizationStatus];
5.每次啟動(dòng)時(shí)可以先判斷用戶是否曾經(jīng)允許過(guò)
[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分鐘不超過(guò)一次通知
//[self.locationManager startMonitoringSignificantLocationChanges];
//區(qū)域模式
//[self.locationManage startMonitoringForRegion:<#(nonnull CLRegion *)#>];
//Visit 模式(略)
位置變動(dòng)后的處理
//位置變動(dòng)后的處理
-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
CLLocation * location = locations[0];//當(dāng)前位置
self.locationLabel.text = [NSString stringWithFormat:@"Location: {%lf , %lf} ^%lf) Direction=%lf degree, Floor=%d",
location.coordinate.longitude,//經(jīng)度
location.coordinate.latitude,//緯度
location.altitude,//海拔
location.course,//方向
(int)location.floor.level//樓層
];
}
地圖顯示
實(shí)現(xiàn)
1.打開(kāi)功能(TARGETS)
2.把MKMapView放到界面
地圖坐標(biāo)系理論知識(shí)
iOS里有3個(gè)坐標(biāo)系
地理坐標(biāo)
Mercator投影坐標(biāo)
-
UIView的視圖坐標(biāo)系
//經(jīng)緯度坐標(biāo)系和Mercator投影坐標(biāo)系換算 MKMapPointForCoordinate(<#CLLocationCoordinate2D coordinate#>) MKCoordinateForMapPoint(<#MKMapPoint mapPoint#>)
MKMapView類型
MKMapView 用法
-
基本用法同一般的UIView
-
配置:顯示區(qū)域
//經(jīng)緯度 .region:顯示區(qū)域 .centerCoordinate:不改變區(qū)域大小 //Mercator地圖坐標(biāo) .visibleMapRect,edgePaddings
-
顯示用戶位置
.showsUserLocation
委托.delegate
-
MKMapView 標(biāo)注
-
MKPoinAnnotation
.coordinate//放哪里 .title/.subtitle//默認(rèn)選中信息 [MKMapView addAnnotation:];//加載
MKAnnotationView:怎么展示
content:image or subclass , .canShowCallout
mapView.delegate供應(yīng):mapView:viewForAnnotation:
標(biāo)準(zhǔn)視圖 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;//移動(dòng)地圖位置
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];