首页 > 代码库 > iOS地图相关知识总结
iOS地图相关知识总结
1、CLLocationManager使用注意细节:
CLLocationManager:表示某个位置的管理者
CLLocation:表示某个位置的地理信息,比如经纬度、海拔等
开始用户定位:
- (void)startUpdatingLocation;
停止用户定位:
- (void) stopUpdatingLocation;
定位改变授权状态:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
定位成功后将进入此方法,开始不断地定位用户的位置,中途会频繁地调用代理的下面方法:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;
定位失败后将进入此方法:
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
计算2个位置之间的距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;
2、相关属性:
每隔多少米定位一次
@property(assign, nonatomic) CLLocationDistance distanceFilter;
定位精确度(越精确就越耗电)
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationDirection course;
行走速度(单位是m/s)
@property(readonly, nonatomic) CLLocationSpeed speed;
3、设置地图中心点和范围:
//CLLocationCoordinate2D是一个用来表示经纬度的结构体
CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(lati, longi);
- MKCoordinateRegion用来设置坐标显示范围。包括两部分:
- a. Center(CLLocationCoordinate2D struct,包括latitude和longitude),坐标中心
- b. Span(MKCoordinateSpan struct,包括latitudeDelta和longitudeDelta),缩放级别
- //创建一个以center为中心,上下各1000米,左右各1000米得区域,但其是一个矩形,不符合MapView的横纵比例
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(center,2000, 2000);
- //以上代码创建出来一个符合MapView横纵比例的区域
- MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
- //以上代码为:最终显示该区域
- [mapView setRegion:adjustedRegion animated:YES];
有三种方法:
//设置中心点和范围
MKCoordinateSpan span=MKCoordinateSpanMake(0.1, 0.1);//地理位置的跨度
MKCoordinateRegion region = MKCoordinateRegionMake(centerCoord, span);//地理位置的坐标区域
//设置中心和范围
MKCoordinateRegion region=MKCoordinateRegionForMapRect(MKMapRect rect)
//设置中心点和以米为单位的经纬度,即在中心点,地图显示维度方向的总距离,经度范围的总距离
MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(centerCoord, 30*10000, 30*10000);
中心点半径转换为MKCoordinateRegion
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, radius, radius);
MapRect转换为MKCoordinateRegion
MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
矩形区域从视图到地图之间转换
- (CGRect)convertRegion:(MKCoordinateRegion)region toRectToView:(UIView *)view;
- (MKMapRect)mapRectForRect:(CGRect)rect;
4、MKMapView使用注意细节:
设置地图显示类型:
- mapView.mapType = MKMapTypeStandard;
- mapView.mapType = MKMapTypeSatellite;
- mapView.mapType = MKMapTypeHybrid;
设置为可以显示用户位置:
- mapView.showsUserLocation = YES;
- 判断用户当前位置是否可见(只读属性):
- userLocationVisible
- 设置用户位置坐标:当userLocationVisible为YES时
- CLLocationCoordinate2D coords = mapView.userLocation.location.coordinate;
MKMapView包括以下属性:
//设置代理
_mapView.delegate = self;
//用户位置
_mapView.showsUserLocation=NO;
//是否滚动
_mapView.scrollEnabled=YES;
//缩放
_mapView.zoomEnabled=YES;
//旋转
_mapView.rotateEnabled=NO;
//倾斜
_mapView.pitchEnabled=NO;
//地图追踪模式
_mapView.userTrackingMode = MKUserTrackingModeFollow;
MKMapViewDelegate包括以下方法:
对地图位置改变的响应:
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
- - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
加载地图数据:
-(void)mapViewWillStartLoadingMap:(MKMapView *)mapView;
-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView;
-(void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error;
渲染地图数据:
-(void)mapViewWillStartRenderingMap:(MKMapView *)mapView;
-(void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered;
追踪用户位置:
-(void)mapViewWillStartLocatingUser:(MKMapView *)mapView;
-(void)mapViewDidStopLocatingUser:(MKMapView *)mapView;
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;
-(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error;
-(void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;
管理标注view:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
拖拽标注view:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState;
选择标注view:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view;
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view;
管理图层显示:
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay;
-(void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray<MKOverlayRenderer *> *)renderers;
管理图层view:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay;
- (void)mapView:(MKMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews;
iOS地图相关知识总结