您的位置:首页 > 移动开发 > IOS开发

iOS中地图功能的实现

2015-10-20 13:11 651 查看
部分摘自iOS开发系列–地图与定位

前言:

地图功能一般是由Map Kit框架中的API来提供的,而定位功能一般是由Core Location框架中的API来提供的。

iOS从6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的。

在iOS中进行地图开发主要有两种方式

直接利用
MapKit
框架进行地图开发,利用这种方式可以对地图进行精准的控制

直接调用苹果官方自带的地图应用,主要用于一些简单的地图应用(例如:进行导航覆盖物填充等),无法进行精确的控制

使用
MapKit
的功能的话,首先引入
MapKit.framework
这个框架,然后导入头文件
#import <MapKit/MapKit.h>


iOS地图主要涉及到两个主要方面的知识:

用户位置跟踪

大头针视图标识

一.用户位置跟踪

在很多带有地图的应用中默认打开地图都会显示用户当前位置,同时将当前位置标记出来放到屏幕中点方便用户对周围情况进行查看。

在iOS6或者iOS7中

只需要添加地图控件、设置用户跟踪模式,代理方法中设置地图中心区域及显示范围

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation


在iOS8中

<1>由于在地图中进行用户位置跟踪需要使用定位功能,而定位功能在iOS8中设计发生了变化,因此必须进行相关的配置和请求。

<2>iOS8中不需要进行中心点的指定,默认会将当前位置设置中心点并自动设置显示区域范围。

<3>下面的代理方法只有在定位到当前位置之后就会调用,以后每当用户位置发生改变就会触发,调用频率相当频繁。

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation


二.大头针视图标识

在iOS地图开发中经常会标记某个位置,需要使用地图标注,也就是大家俗称的“大头针”。只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中
CLLocationCoordinate2D coordinate;
(标记位置)、
NSString *title;
(标题)、
NSString *subtitle;
(子标题)三个属性,然后在程序中创建大头针对象并调用
addAnnotation:
方法添加大头针即可

代码展示(不涉及大头针):

先在
info.plist
中配置:



#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<MKMapViewDelegate>

@property(nonatomic,strong)CLLocationManager* locManager;
@property(nonatomic,strong)MKMapView* map;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.map=[[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.map];
self.map.delegate=self;

self.locManager=[[CLLocationManager alloc] init];
[self.locManager requestWhenInUseAuthorization];
self.map.userTrackingMode=MKUserTrackingModeFollow;
self.map.mapType=MKMapTypeStandard;

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"%@",userLocation);
}

@end


运行结果如下:



下面再展示一下使用大头针的视图:

建一个模型文件:

SSannotation.h
文件

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface SSannotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end


与第一个展示代码一样,只是添加了大头针。

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "SSannotation.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<MKMapViewDelegate>

@property(nonatomic,strong)CLLocationManager* locManager;
@property(nonatomic,strong)MKMapView* map;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.map=[[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.map];
self.map.delegate=self;

self.locManager=[[CLLocationManager alloc] init];
[self.locManager requestWhenInUseAuthorization];
self.map.userTrackingMode=MKUserTrackingModeFollow;
self.map.mapType=MKMapTypeStandard;

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
SSannotation* annotation=[[SSannotation alloc] init];

annotation.title=@"me";
annotation.subtitle=@"whut";
annotation.coordinate=userLocation.coordinate;
[self.map addAnnotation:annotation];
}




只有点击大头针,才会出现标题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: