iOS在地图上画图和导航

//

//  ViewController.m

//  navitomyhome

//

//  Created by tianshangrenjian on 15/6/23.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//


#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *map;

@property (strong, nonatomic) CLPlacemark *destPos;

@property (strong,nonatomic) CLLocationManager *mgr;

@property (strong,nonatomic) CLGeocoder *geocoder;

@end


@implementation ViewController

- (CLLocationManager *)mgr

{

    if (_mgr==nil) {

        _mgr=[[CLLocationManager alloc] init];

    }

    return _mgr;

}

- (CLPlacemark *)destPos

{

    if (_destPos==nil) {

        _destPos=[[CLPlacemark alloc] init];

    }

    return _destPos;

}

- (CLGeocoder *)geocoder

{

    if (_geocoder==nil) {

        _geocoder=[[CLGeocoder alloc] init];

    }

    return _geocoder;

}

- (void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray *)locations

{

    

}

- (void)drawLineWithPlaceMark:(MKPlacemark*)sourcePlace andDestPlaceMark:(MKPlacemark*)destPlaceMark

{

    MKDirectionsRequest *mkRequest=[[MKDirectionsRequest alloc] init];

    MKMapItem *sourceItem=[[MKMapItem alloc] initWithPlacemark:sourcePlace];

    

    MKMapItem *destItem=[[MKMapItem alloc] initWithPlacemark:destPlaceMark];

    

    mkRequest.destination=destItem;

    mkRequest.source=sourceItem;

    

    MKDirections *directions =    [[MKDirections alloc]initWithRequest:mkRequest];

    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * __nullable response, NSError * __nullable error) {

        

        for (MKRoute *route in response.routes) {

            [self.map addOverlay:route.polyline];

        }

    }];

}

- (nonnull MKOverlayRenderer *)mapView:(nonnull MKMapView *)mapView rendererForOverlay:(nonnull id<MKOverlay>)overlay

{

    MKPolylineRenderer *line1=[[MKPolylineRenderer alloc]initWithOverlay:overlay];

    line1.lineWidth=4;

    line1.strokeColor=[UIColor redColor];

    

 //   MKOverlayRenderer *lay=[[MKOverlayRenderer alloc] initWithOverlay:overlay];

    

    return line1;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.map.delegate=self;

    [self.geocoder geocodeAddressString:@"武汉" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {

        __block CLPlacemark *fromPlace=[placemarks firstObject];

       [self.geocoder geocodeAddressString:@"北京" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {

           MKPlacemark *mkfromplace=[[MKPlacemark alloc] initWithPlacemark:fromPlace];

           [self drawLineWithPlaceMark:mkfromplace andDestPlaceMark:[[MKPlacemark alloc] initWithPlacemark:[placemarks firstObject]]];

       }];

    }];

  

//    [self.mgr startUpdatingLocation];

//    [self.geocoder geocodeAddressString:@"玉溪市" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {

//        if (error || placemarks.count==0) {

//            return ;

//        }

//        self.destPos=[placemarks firstObject];

//    }];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)goHome:(id)sender {

    MKPlacemark *mkPlacemark=[[MKPlacemark alloc] initWithPlacemark:self.destPos];

    MKMapItem *item1=[[MKMapItem alloc ] initWithPlacemark:mkPlacemark];

    

    NSMutableDictionary *dict=[NSMutableDictionary dictionary];

    

    dict[MKLaunchOptionsDirectionsModeKey]=MKLaunchOptionsDirectionsModeDriving;

    dict[MKLaunchOptionsShowsTrafficKey]=@YES;

    

    [MKMapItem openMapsWithItems:@[item1] launchOptions:dict];

    

    

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值