首页 > 代码库 > iOS地图相关知识总结

iOS地图相关知识总结

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC Semibold"; color: #ed719e } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #ed719e } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px "PingFang SC"; color: #000000 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px Verdana; color: #000000 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px Verdana; color: #000000; min-height: 17.0px } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000; min-height: 20.0px } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000 } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p11 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 18.0px "PingFang SC"; color: #000000; min-height: 25.0px } p.p12 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p13 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 21.0px; font: 14.0px "PingFang SC"; color: #000000 } p.p14 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #000000; min-height: 25.0px } p.p16 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 21.0px; font: 14.0px "PingFang SC"; color: #333333 } p.p17 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px "PingFang SC"; color: #333333 } p.p18 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px "PingFang SC"; color: #333333; min-height: 20.0px } p.p19 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 13.0px "PingFang SC"; color: #333333 } p.p20 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px Arial; color: #333333 } p.p21 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px "PingFang SC"; color: #000000; min-height: 20.0px } p.p22 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 21.0px; font: 14.0px Courier; color: #103bff } p.p23 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 21.0px; font: 14.0px Courier; color: #494949 } p.p24 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 21.0px; font: 14.0px "PingFang SC"; color: #103bff } p.p25 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px Courier; color: #494949 } p.p26 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 22.0px; font: 17.0px "Microsoft YaHei"; color: #333333 } p.p27 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #008400 } p.p28 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187 } p.p29 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa } p.p30 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p31 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3e1e81 } p.p32 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 22.0px; font: 14.0px "PingFang SC"; color: #333333 } p.p34 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 22.0px; font: 14.0px "PingFang SC"; color: #333333; min-height: 20.0px } p.p35 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p36 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 22.0px; font: 14.0px "Microsoft YaHei"; color: #333333 } p.p37 { margin: 0.0px 0.0px 0.0px 0.0px; font: 17.0px "Microsoft YaHei"; color: #333333; min-height: 22.0px } li.li7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } li.li9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000 } li.li15 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 18.0px; font: 14.0px "PingFang SC"; color: #000000 } li.li33 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #333333; min-height: 20.0px } span.s1 { } span.s2 { font: 14.0px "PingFang SC" } span.s3 { } span.s4 { color: #ba2da2 } span.s5 { color: #703daa } span.s6 { font: 14.0px "PingFang SC" } span.s7 { color: #3e1e81 } span.s8 { color: #ff0000 } span.s9 { color: #0000ff } span.s10 { color: #000000 } span.s11 { font: 14.0px "PingFang SC"; color: #444444 } span.s12 { font: 14.0px SimSun } span.s13 { font: 14.0px Menlo } span.s14 { color: #000000 } span.s15 { color: #4f8187 } span.Apple-tab-span { white-space: pre } ol.ol1 { list-style-type: decimal } ul.ul1 { list-style-type: disc }</style>

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);

 

  1. MKCoordinateRegion用来设置坐标显示范围。包括两部分:  
  2. a. Center(CLLocationCoordinate2D struct,包括latitude和longitude),坐标中心  
  3. b. Span(MKCoordinateSpan struct,包括latitudeDelta和longitudeDelta),缩放级别  
  4.   
  5. //创建一个以center为中心,上下各1000米,左右各1000米得区域,但其是一个矩形,不符合MapView的横纵比例  
  6. MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(center,2000, 2000);  
  7. //以上代码创建出来一个符合MapView横纵比例的区域  
  8. MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];   
  9. //以上代码为:最终显示该区域  
  10. [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使用注意细节:

设置地图显示类型:

  1. mapView.mapType = MKMapTypeStandard;  
  2. mapView.mapType = MKMapTypeSatellite;  
  3. mapView.mapType = MKMapTypeHybrid;   

 

设置为可以显示用户位置:  

  1. mapView.showsUserLocation = YES;   
  2. 判断用户当前位置是否可见(只读属性):  
  1. userLocationVisible   
  2. 设置用户位置坐标:当userLocationVisible为YES时  
  3. 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地图相关知识总结