一、mapkit的基本使用
#import "ViewController.h"
#import
#import
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
/** */
@property (nonatomic, strong) CLLocationManager *lM;
@end
@implementation ViewController
- (CLLocationManager *)lM
{
if (!_lM) {
_lM = [[CLLocationManager alloc] init];
if ([_lM respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[_lM requestAlwaysAuthorization];
}
}
return _lM;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
/**
MKMapTypeStandard = 0,
MKMapTypeSatellite,
MKMapTypeHybrid,
MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
*/
// self.mapView.mapType = MKMapTypeSatelliteFlyover;
// self.mapView.zoomEnabled = NO;
//
//// self.mapView.showsCompass = NO;
// self.mapView.showsScale = YES;
[self lM];
// self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
}
@end
二、mapkit的中级使用
#import "ViewController.h"
#import
#import
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
/** */
@property (nonatomic, strong) CLLocationManager *lM;
@end
@implementation ViewController
- (CLLocationManager *)lM
{
if (!_lM) {
_lM = [[CLLocationManager alloc] init];
if ([_lM respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[_lM requestAlwaysAuthorization];
}
}
return _lM;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
/**
MKMapTypeStandard = 0,
MKMapTypeSatellite,
MKMapTypeHybrid,
MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
*/
// self.mapView.mapType = MKMapTypeSatelliteFlyover;
// self.mapView.zoomEnabled = NO;
//
//// self.mapView.showsCompass = NO;
// self.mapView.showsScale = YES;
[self lM];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
// self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
}
#pragma mark - MKMapViewDelegate
/**
* 更新到位置
*
* @param mapView 地图
* @param userLocation 位置对象
*/
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/**
* MKUserLocation (大头针模型)
*
*
*/
userLocation.title = @"银江软件园";
userLocation.subtitle = @"城市宝";
// 设置地图显示中心
// [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
// 设置地图显示区域
MKCoordinateSpan span = MKCoordinateSpanMake(0.051109, 0.034153);
MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
[self.mapView setRegion:region animated:YES];
}
三、大头针的添加
#import "ViewController.h"
#import
#import
#import "XMGAnno.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
/** <#注释#> */
@property (nonatomic, strong) CLGeocoder *geoC;
@end
@implementation ViewController
- (CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1. 获取当前触摸点
CGPoint point = [[touches anyObject] locationInView:self.mapView];
// 2. 转换成经纬度
CLLocationCoordinate2D pt = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// 3. 添加大头针
[self addAnnoWithPT:pt];
}
//-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//{
// // 移除大头针(模型)
// NSArray *annos = self.mapView.annotations;
// [self.mapView removeAnnotations:annos];
//}
- (void)addAnnoWithPT:(CLLocationCoordinate2D)pt
{
__block XMGAnno *anno = [[XMGAnno alloc] init];
anno.coordinate = pt;
anno.title = @"银江软件园";
anno.subtitle = @"城市宝";
anno.type = arc4random_uniform(5);
[self.mapView addAnnotation:anno];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:anno.coordinate.latitude longitude:anno.coordinate.longitude];
[self.geoC reverseGeocodeLocation:loc completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *pl = [placemarks firstObject];
anno.title = pl.locality;
anno.subtitle = pl.thoroughfare;
}];
// 添加多个大头针
// self.mapView addAnnotations:<#(nonnull NSArray> *)#>
}
#pragma mark - MKMapViewDelegate
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}
/**
* 当我们添加大头针模型时,
*
* @param mapView 地图
* @param annotation 大头针
*
* @return 大头针视图
*/
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
// return nil;
static NSString *inden = @"datouzhen";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:inden];
if (pin == nil) {
pin = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:inden];
}
pin.annotation = annotation;
// 设置是否弹出标注
pin.canShowCallout = YES;
XMGAnno *anno = (XMGAnno *)annotation;
NSString *imageName = [NSString stringWithFormat:@"category_%zd", anno.type + 1];
pin.image = [UIImage imageNamed:imageName];
// 设置大头针图片(系统大头针无效)
// pin.image = [UIImage imageNamed:@"category_5"];
pin.draggable = YES;
// pin.calloutOffset = CGPointMake(5, 8);
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
iv.image = [UIImage imageNamed:@"htl"];
pin.leftCalloutAccessoryView = iv;
UIImageView *ivR = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
ivR.image = [UIImage imageNamed:@"eason"];
pin.rightCalloutAccessoryView = ivR;
pin.detailCalloutAccessoryView = [UISwitch new];
return pin;
}
- (MKPinAnnotationView *)systemAnnoWithMapView:(MKMapView *)mapView andAnno:(id)annotation
{
static NSString *inden = @"datouzhen";
MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:inden];
if (pin == nil) {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:inden];
}
pin.annotation = annotation;
// 设置是否弹出标注
pin.canShowCallout = YES;
// 设置大头针颜色
pin.pinTintColor = [UIColor blackColor];
// 从天而降
pin.animatesDrop = YES;
// 设置大头针图片(系统大头针无效)
// pin.image = [UIImage imageNamed:@"category_5"];
pin.draggable = YES;
return pin;
}
// 不选中
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
NSLog(@"不选中");
NSLog(@"%@", view.annotation);
}
// 选中
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(@"选中");
}
@end
四、利用系统App导航|3D视角|地图快照截图
#import "ViewController.h"
#import
@interface ViewController ()
/** */
@property (nonatomic, strong) CLGeocoder *geoC;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 3D视角
// MKMapCamera *camer = [MKMapCamera cameraLookingAtCenterCoordinate:CLLocationCoordinate2DMake(23.132931, 113.375924) fromEyeCoordinate:CLLocationCoordinate2DMake(23.135931, 113.375924) eyeAltitude:10];
// self.mapView.camera = camer;
//地图快照截图
MKMapSnapshotOptions *option = [[MKMapSnapshotOptions alloc] init];
// 针对地图
option.region = self.mapView.region;
option.showsBuildings = YES;
// 输出图片
option.size = CGSizeMake(1000, 2000);
option.scale = [UIScreen mainScreen].scale;
MKMapSnapshotter *snap = [[MKMapSnapshotter alloc] initWithOptions:option];
[snap startWithCompletionHandler:^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
if (error == nil) {
UIImage *image = snapshot.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:@"/Users/xiaomage/Desktop/map.png" atomically:YES];
}else
{
NSLog(@"--%@", error.localizedDescription);
}
}];
}
- (void)begNav
{
[self.geoC geocodeAddressString:@"广州" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
// 广州地标
CLPlacemark *gzP = [placemarks firstObject];
[self.geoC geocodeAddressString:@"上海" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
// 上海地标
CLPlacemark *shP = [placemarks firstObject];
[self beginNavWithBpl:gzP andEndP:shP];
}];
}];
}
- (void)beginNavWithBpl:(CLPlacemark *)beginP andEndP:(CLPlacemark *)endP
{
// 创建开始的地图项
CLPlacemark *clPB = beginP;
MKPlacemark *mkPB = [[MKPlacemark alloc] initWithPlacemark:clPB];
MKMapItem *beginI = [[MKMapItem alloc] initWithPlacemark:mkPB];
// 创建结束的地图项
CLPlacemark *clP = endP;
MKPlacemark *mkP = [[MKPlacemark alloc] initWithPlacemark:clP];
MKMapItem *endI = [[MKMapItem alloc] initWithPlacemark:mkP];
// 地图项数组
NSArray *items = @[beginI, endI];
// 启动字典
NSDictionary *dic = @{
// 导航方式
MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
// 地图类型
MKLaunchOptionsMapTypeKey : @(MKMapTypeHybrid),
// 是否显示交通
MKLaunchOptionsShowsTrafficKey : @(YES)
};
[MKMapItem openMapsWithItems:items launchOptions:dic];
}
@end
五、获取导航路线信息
#import "ViewController.h"
#import
#import
@interface ViewController ()
/** */
@property (nonatomic, strong) CLGeocoder *geoC;
@end
@implementation ViewController
- (CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.geoC geocodeAddressString:@"广州" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *gzP = [placemarks firstObject];
[self.geoC geocodeAddressString:@"shanghai" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *shP = [placemarks firstObject];
[self getRouteWithBeginPL:gzP andEndPL:shP];
}];
}];
}
- (void)getRouteWithBeginPL:(CLPlacemark *)beginP andEndPL:(CLPlacemark *)endPL
{
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
// 起点
CLPlacemark *clP = beginP;
MKPlacemark *mkP = [[MKPlacemark alloc] initWithPlacemark:clP];
MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:mkP];
request.source = sourceItem;
// 终点
CLPlacemark *clP2 = endPL;
MKPlacemark *mkP2 = [[MKPlacemark alloc] initWithPlacemark:clP2];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:mkP2];
request.destination = endItem;
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
/**
* MKDirectionsResponse
routes : 路线数组MKRoute
*/
/**
* MKRoute
name : 路线名称
distance : 距离
expectedTravelTime : 预期时间
polyline : 折线(数据模型)
steps
*/
/**
* steps
instructions : 行走提示
*/
// NSLog(@"%@", response);
[response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@---%zd---%f", obj.name, obj.expectedTravelTime, obj.distance);
[obj.steps enumerateObjectsUsingBlock:^(MKRouteStep * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj.instructions);
}];
}];
}];
}
@end
六、绘制路线信息
#import "ViewController.h"
#import
#import
@interface ViewController ()
/** */
@property (nonatomic, strong) CLGeocoder *geoC;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.geoC geocodeAddressString:@"广州" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *gzP = [placemarks firstObject];
[self.geoC geocodeAddressString:@"shanghai" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *shP = [placemarks firstObject];
[self getRouteWithBeginPL:gzP andEndPL:shP];
}];
}];
}
- (void)getRouteWithBeginPL:(CLPlacemark *)beginP andEndPL:(CLPlacemark *)endPL
{
MKCircle *circle = [MKCircle circleWithCenterCoordinate:beginP.location.coordinate radius:100000];
[self.mapView addOverlay:circle];
MKCircle *circle2 = [MKCircle circleWithCenterCoordinate:endPL.location.coordinate radius:100000];
[self.mapView addOverlay:circle2];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
// 起点
CLPlacemark *clP = beginP;
MKPlacemark *mkP = [[MKPlacemark alloc] initWithPlacemark:clP];
MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:mkP];
request.source = sourceItem;
// 终点
CLPlacemark *clP2 = endPL;
MKPlacemark *mkP2 = [[MKPlacemark alloc] initWithPlacemark:clP2];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:mkP2];
request.destination = endItem;
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
/**
* MKDirectionsResponse
routes : 路线数组MKRoute
*/
/**
* MKRoute
name : 路线名称
distance : 距离
expectedTravelTime : 预期时间
polyline : 折线(数据模型)
steps
*/
/**
* steps
instructions : 行走提示
*/
// NSLog(@"%@", response);
[response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@---%zd---%f", obj.name, obj.expectedTravelTime, obj.distance);
MKPolyline *polyline = obj.polyline;
// 添加一个覆盖层数据模型
[self.mapView addOverlay:polyline];
}];
}];
}
#pragma mark - MKMapViewDelegate
/**
* 获取对应的图层渲染
*
* @param mapView 地图
* @param overlay 覆盖层数据模型
*
* @return 图层渲染
*/
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay
{
if ([overlay isKindOfClass:[MKCircle class]]) {
MKCircleRenderer *circleR = [[MKCircleRenderer alloc] initWithOverlay:overlay];
circleR.fillColor = [UIColor cyanColor];
circleR.alpha = 0.5;
return circleR;
}
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
// 设置线宽
render.lineWidth = 10;
// 设置颜色
render.strokeColor = [UIColor redColor];
return render;
}
return nil;
}