地图导航划线

简单是ios地图划线事例 有上一篇关于地图的注释,着个应该很清楚了
//
//  ViewController.m
//  2014_11_03_地图划线
//
//  Created by Mac10.9 on 14-11-3.
//  Copyright (c) 2014年 xiaoxiaobing. All rights reserved.
//

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

#import "XXBAnnotation.h"

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate]] >
//为了开启定位服务员
@property (nonatomic, strong) CLLocationManager *locMgr;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLGeocoder *geocoder;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupCLLocationManager];
    [self setupMapView];
}

/**
 *  懒加载
 */
- (CLLocationManager *)locMgr
{
#warning 定位服务不可用
    if(![CLLocationManager locationServicesEnabled])
    {
        /**
         只有系统的定位功能是关闭的时候才会调用这个方法
         */
        return nil;
    }
   
    if (!_locMgr)
    {
        // 创建定位管理者
        self.locMgr = [[CLLocationManager alloc] init];
        // 设置代理
        self.locMgr.delegate = self;
    }
    return _locMgr;
}

- (void)setupCLLocationManager
{
    // 开始定位用户的位置
    [self.locMgr startUpdatingLocation];
    [self.locMgr requestAlwaysAuthorization];
}
- (CLGeocoder *)geocoder
{
    if (!_geocoder) {
        self.geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

- (void)setupMapView
{
    self.mapView.delegate = self;
   
    NSString *address1 = @"北京";
    NSString *address2 = @"广州";
   
    [self.geocoder geocodeAddressString:address1 completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) return;
       
        CLPlacemark *fromPm = [placemarks firstObject];
       
        [self.geocoder geocodeAddressString:address2 completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error) return;
           
            CLPlacemark *toPm = [placemarks firstObject];
           
            [self addLineFrom:fromPm to:toPm];
        }];
    }];
}
/**
 *  添加导航的线路
 *
 *  @param fromPm 起始位置
 *  @param toPm   结束位置
 */
- (void)addLineFrom:(CLPlacemark *)fromPm to:(CLPlacemark *)toPm
{
    // 1.添加2个大头针
    XXBAnnotation *fromAnno = [[XXBAnnotation alloc] init];
    fromAnno.coordinate = fromPm.location.coordinate;
    fromAnno.title = fromPm.name;
    [self.mapView addAnnotation:fromAnno];
   
    XXBAnnotation *toAnno = [[XXBAnnotation alloc] init];
    toAnno.coordinate = toPm.location.coordinate;
    toAnno.title = toPm.name;
    [self.mapView addAnnotation:toAnno];
   
    // 2.查找路线
   
    // 方向请求
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    // 设置起点
    MKPlacemark *sourcePm = [[MKPlacemark alloc] initWithPlacemark:fromPm];
    request.source = [[MKMapItem alloc] initWithPlacemark:sourcePm];
   
    // 设置终点
    MKPlacemark *destinationPm = [[MKPlacemark alloc] initWithPlacemark:toPm];
    request.destination = [[MKMapItem alloc] initWithPlacemark:destinationPm];
   
    // 方向对象
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
   
    // 计算路线
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        NSLog(@"总共%lu条路线", (unsigned long)response.routes.count);
       
        // 遍历所有的路线
        for (MKRoute *route in response.routes) {
            // 添加路线遮盖
            [self.mapView addOverlay:route.polyline];
        }
    }];
}
#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    renderer.strokeColor = [UIColor redColor];
    return renderer;
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值