地圖Mapkit 繪製路線

來源:互聯網
上載者:User

標籤:copyright   rand   enter   esb   with   iba   reg   gre   dex   

////  ViewController.m//  MapDrawLine////  Created by 謝澤鋒 on 2016/10/27.//  Copyright ? 2016年 xiezefeng. All rights reserved.//#import "ViewController.h"#import <MapKit/MapKit.h>@interface ViewController ()<MKMapViewDelegate>@property (nonatomic, strong) MKPolyline *routeLine;@property (nonatomic, strong) NSMutableArray *mutArray;@property (nonatomic, strong) CLGeocoder *geocoder;@property(nonatomic,strong)MKMapView * mapView;@end@implementation ViewController- (NSMutableArray *)mutArray {    if (_mutArray == nil) {        _mutArray = [NSMutableArray array];    }    return _mutArray;}- (CLGeocoder *)geocoder {    if (!_geocoder) {        self.geocoder = [[CLGeocoder alloc] init];    }    return _geocoder;}- (void)viewDidLoad {    [super viewDidLoad];    _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*2/3)];       _mapView.delegate= self;    CLLocation *location1 = [[CLLocation alloc] initWithLatitude:23.4 longitude:113.3];//    CLLocation *location2 = [[CLLocation alloc] initWithLatitude:23.6 longitude:113.9];//    CLLocation *location3 = [[CLLocation alloc] initWithLatitude:23.0 longitude:113.4];//    CLLocation *location4 = [[CLLocation alloc] initWithLatitude:23.7 longitude:113.7];    [self.mutArray addObject:location1];//    [self.mutArray addObject:location2];//    [self.mutArray addObject:location3];//    [self.mutArray addObject:location4];    [self.view addSubview:_mapView];            MKCoordinateRegion region;//    region.span = MKCoordinateSpanMake(1, 1);    region.center = CLLocationCoordinate2DMake(location1.coordinate.latitude,location1.coordinate.longitude);    [self.mapView setRegion:region animated:YES];        [self drawLineWithLocationArray:self.mutArray];        // Do any additional setup after loading the view, typically from a nib.}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    double num = 0.1*(arc4random()%10);    double num1 =0.1*(arc4random()%10);    CLLocation *location4 = [[CLLocation alloc] initWithLatitude:23.7+num longitude:113.7+num1];    [self.mutArray addObject:location4];    [self drawLineWithLocationArray:self.mutArray];}- (IBAction)ac:(id)sender {    [self.mutArray removeAllObjects];    [self drawLineWithLocationArray:self.mutArray];}//地圖畫線- (void)drawLineWithLocationArray:(NSMutableArray *)locationArray{                MKMapPoint* pointArray = malloc(sizeof(CLLocationCoordinate2D) * locationArray.count);        for(int idx = 0; idx < locationArray.count; idx++)    {        CLLocation *location = [locationArray objectAtIndex:idx];                CLLocationDegrees latitude  = location.coordinate.latitude;                CLLocationDegrees longitude = location.coordinate.longitude;                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);                MKMapPoint point = MKMapPointForCoordinate(coordinate);                pointArray[idx] = point;    }        if (_routeLine) {        [_mapView removeOverlay:_routeLine];    }        _routeLine = [MKPolyline polylineWithPoints:pointArray count:locationArray.count];        if (nil != _routeLine) {        [_mapView addOverlay:_routeLine];    }        free(pointArray);}-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];        renderer.strokeColor = [UIColor blueColor];        renderer.lineWidth = 2;        return renderer;    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

地圖Mapkit 繪製路線

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.