美文网首页生活成长史地图iOS学习笔记
iOS 系统自带API地图开发相关(一)

iOS 系统自带API地图开发相关(一)

作者: 楚简约 | 来源:发表于2017-04-27 10:51 被阅读760次

系统自带API

CoreLocation

 #import <CoreLocation/CoreLocation.h>  用于地理定位

CoreLocation框架使用须知
CoreLocation框架中所有数据类型的前缀都是CL
CoreLocation中使用CLLocationManager对象来做用户定位

1.CLLocationManager

常用操作

- (void)startUpdatingLocation;   //开始用户定位
- (void) stopUpdatingLocation;  //停止用户定位

当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
locations参数里面装着CLLocation对象

CLLocationManager的相关属性

//每隔多少米定位一次
@property(assign, nonatomic) CLLocationDistance distanceFilter;
//定位精确度(越精确就越耗电)
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

CLLocationManagerDelegate代理方法的简单讲解链接-可点击


2.CLLocationCoordinate2D

是一个用来表示经纬度的结构体,定义如下

typedef struct {
        CLLocationDegrees latitude; // 纬度
        CLLocationDegrees longitude; // 经度
} CLLocationCoordinate2D;

一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([_latitude doubleValue],[_longitude doubleValue]);
//其中_latitude和_longitude是以字符串NSString的形式存在传递
3.CLLocation

用来表示某个位置的地理信息,比如经纬度、海拔等等

//经纬度
@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;

用下面方法可以计算2个位置之间的距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

这个地方提一点在模拟器上,需要设置模拟位置 ,最好真机测试
帝都的经纬度是:北纬40°,东经116°


模拟器模拟位置.png

当然还要注意配置info.plist文件
是否允许xxxxxxxxx访问你的地理位置
Privacy - Location Always Usage Description //总是使用用户位置
Privacy - Location When In Use Usage Description//使用应用时定位

为了严谨起见,最好在使用定位功能之前判断当前应用的定位功能是否可用
CLLocationManager有个类方法可以判断当前应用的定位功能是否可用

+ (BOOL)locationServicesEnabled;
4.CLGeocoder

使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息

地理编码方法
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

反地理编码方法
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

5.CLPlacemark

CLPlacemark的字面意思是地标,封装详细的地址位置信息

//地理位置
@property (nonatomic, readonly) CLLocation *location;
//区域
@property (nonatomic, readonly) CLRegion *region;
//详细的地址信息
@property (nonatomic, readonly) NSDictionary *addressDictionary;
//地址名称
@property (nonatomic, readonly) NSString *name;
//城市
@property (nonatomic, readonly) NSString *locality;
//省/州
@property (nonatomic, readonly, copy, nullable) NSString *administrativeArea;
CLPlacemark.png

注意:
①.四大直辖市的城市信息无法通过CLPlacemark的locality属性获得,只能通过访问administrativeArea属性来获得(如果locality为空,则可知为直辖市),代码参考如下

  NSString *city = placemark.locality;

     if (!city) {

           //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

          city = placemark.administrativeArea;

     }

②.上面的FormattedAddressLines可通过以下方法输出:

  NSArray *lines = _placemark.addressDictionary[@"FormattedAddressLines"];

  NSString *addressString = [lines componentsJoinedByString:@"\n"];

  NSLog(@"Address: %@", addressString);

③.星号表示addressDictionary里面可能没有这个键和值


--------------------------- 华丽的分割线 ----------------------------

MapKit.

 #import <MapKit/MapKit.h>   用于地图展示
MKMapView

跟踪显示用户的位置
设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置

MKUserTrackingModeNone :不跟踪用户的位置
MKUserTrackingModeFollow :跟踪并在地图上显示用户的当前位置
MKUserTrackingModeFollowWithHeading :跟踪并在地图上显示用户的当前位置,地图会跟随用户的前进方向进行旋转

下图是跟踪效果
蓝色发光圆点就是用户的当前位置
蓝色发光原点,专业术语叫做“大头针”


跟踪效果.png

地图的类型
可以通过设置MKMapView的mapViewType设置地图类型

MKMapTypeStandard :普通地图
MKMapTypeSatellite :卫星云图 
MKMapTypeHybrid :普通地图覆盖于卫星云图之上

iOS9的一些新特性

    //显示指南针
    self.mapView.showsCompass = YES;
    //显示比例尺
    self.mapView.showsScale = YES;
    //显示交通状况
    self.mapView.showsTraffic = YES;
    //显示建筑物
    self.mapView.showsBuildings = YES;
    //显示用户所在的位置
    self.mapView.showsUserLocation = YES;
    //显示感兴趣的东西
    self.mapView.showsPointsOfInterest = YES;
MKMapView的代理

MKMapView可以设置一个代理对象,用来监听地图的相关行为

常见的代理方法有
//一个位置更改默认只会调用一次,不断监测用户的当前位置
//每次调用,都会把用户的最新位置(userLocation参数)传进来
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;

//地图的显示区域即将发生改变的时候调用
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;

//地图的显示区域已经发生改变的时候调用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;

MKUserLocation
MKUserLocation其实是个大头针模型,包括以下属性

//显示在大头针上的标题
@property (nonatomic, copy) NSString *title;
//显示在大头针上的子标题
@property (nonatomic, copy) NSString *subtitle;
地理位置信息(大头针钉在什么地方?) (注意这个是只读属性)
@property (readonly, nonatomic) CLLocation *location;

至于自定义大头针之后再讲

通过MKMapView的下列方法,可以设置地图显示的位置和区域

//设置地图的中心点位置
@property (nonatomic) CLLocationCoordinate2D centerCoordinate;
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated;
//设置地图的显示区域
@property (nonatomic) MKCoordinateRegion region;
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
MKCoordinateRegion

MKCoordinateRegion是一个用来表示区域的结构体,定义如下

typedef struct {
          CLLocationCoordinate2D center; // 区域的中心点位置
        MKCoordinateSpan span; // 区域的跨度
} MKCoordinateRegion;

MKCoordinateSpan的定义
typedef struct {
    CLLocationDegrees latitudeDelta; // 纬度跨度
    CLLocationDegrees longitudeDelta; // 经度跨度
} MKCoordinateSpan;

    //设定显示范围
    MKCoordinateSpan theSpan;
    theSpan.latitudeDelta=0.01;
    theSpan.longitudeDelta=0.01;
    //设置地图显示的中心及范围
    MKCoordinateRegion theRegion;
    theRegion.center=theCoordinate;
    theRegion.span=theSpan;
    [_mapView setRegion:theRegion];
    //返回我的位置
    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
关于地图大头针基本操作
//添加一个大头针
- (void)addAnnotation:(id <MKAnnotation>)annotation;

//添加多个大头针
- (void)addAnnotations:(NSArray *)annotations;

//移除一个大头针
- (void)removeAnnotation:(id <MKAnnotation>)annotation;

//移除多个大头针
- (void)removeAnnotations:(NSArray *)annotations;

(id <MKAnnotation>)annotation参数是什么东西?
大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据

添加大头针

MKAnnotation *anno = [[MKAnnotation alloc] init];
anno.title = @"我是一个大头针";
anno.subtitle = @"我有一个小弟叫小头";
anno.coordinate = CLLocationCoordinate2DMake(40, 116);
[self.mapView addAnnotation:anno];

关于自定义大头针有两种思路:

一.自定大头针模型MKAnnotation
二.自定义大头针控件MKAnnotationView

之后专门建一篇文章讲解吧

MKAnnotationView

地图上的大头针控件是MKAnnotationView

MKAnnotationView的属性

//大头针模型
@property (nonatomic, strong) id <MKAnnotation> annotation;
//显示的图片
@property (nonatomic, strong) UIImage *image;
//是否显示标注
@property (nonatomic) BOOL canShowCallout;
//标注的偏移量
@property (nonatomic) CGPoint calloutOffset;
//标注右边显示什么控件
@property (strong, nonatomic) UIView *rightCalloutAccessoryView;
//标注左边显示什么控件
@property (strong, nonatomic) UIView *leftCalloutAccessoryView;
MKPinAnnotationView

MKPinAnnotationView是MKAnnotationView的子类
MKPinAnnotationView比MKAnnotationView多了2个属性

//大头针颜色
@property (nonatomic) MKPinAnnotationColor pinColor;
//大头针第一次显示时是否从天而降
@property (nonatomic) BOOL animatesDrop;

注:如果想创建以静态图片作为大头针图片的话,可以通过创建MKAnnotationView是实例。如果想使用apple自带的大头针则创建MKPinAnnotationView

MKAnnotationView 大头针控件 是所有大头针试图控件的父类(可以随意更改图片和颜色)
MKPinAnnotationView 大头的视图针控件,是MKAnnotationView的子类(不能改变大头针视图的图片,可以改变颜色)


iOS 系统自带API地图开发相关(一)
iOS 系统自带API地图开发相关(二)
iOS 系统自带API地图开发相关(三)


第一节到此为止.记录下来同大家分享!!!🙂🙂🙂

我是楚简约,感谢您的阅读,

喜欢就点个赞呗,“❤喜欢”,

鼓励又不花钱,你在看,我就继续写~

非简书用户,可以点右上角的三个“...”,然后"在Safari中打开”,就可以点赞咯~


相关文章

网友评论

    本文标题:iOS 系统自带API地图开发相关(一)

    本文链接:https://www.haomeiwen.com/subject/pergzttx.html