地图Mapkit 绘制路线

//
//  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
时间: 2024-08-24 07:50:45

地图Mapkit 绘制路线的相关文章

导航 -MapKit - 获取路线信息绘制导航路线

#import "PPViewController.h" #import <MapKit/MapKit.h> #import "PPAnnotation.h" @interface PPViewController ()<MKMapViewDelegate> /** *  编码对象 */ @property (nonatomic, strong) CLGeocoder *geocoder; @property (weak, nonatomic

【 D3.js 入门系列 --- 10 】 地图的绘制

本人的个人博客为:www.ourd3js.com csdn博客为:blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 地图的制作在 D3 中可以说是最重要的一环.因为在进行数据可视化时,很多情况都会和地图联系在一起,如中国各省的人口多少,GDP多少等,都可以和地图联系在一起. D3 中制作地图所需要的文件问 JSON 文件.JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.关于 JSON 的语法格式,可以在: http://www.w3s

iOS开发之在地图上绘制出你运行的轨迹

首先我们看下如何在地图上绘制曲线.在Map Kit中提供了一个叫MKPolyline的类,我们可以利用它来绘制曲线,先看个简单的例子. 使用下面代码从一个文件中读取出经纬度,然后创建一个路径:MKPolyline实例. 1 -(void) loadRoute 2 { 3 NSString* filePath = [[NSBundle mainBundle] pathForResource:@"route" ofType:@"csv"]; 4 NSString* fi

高德地图-设置直线路线

1.问题背景 给点了地图上两个坐标,然后连接两个点,成为一条直线 2.实现源码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content=&qu

地图 MapKit

一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用中,都用到了定位和地图功能,在iOS开发中,要想加入这2大功能,必须基于2个框架进行开发CoreLocation :用于地理定位,地理编码,区域监听等(着重功能实现)MapKit :用于地图展示,例如大头针,路线.覆盖层展示等(着重界面展示) 2.MapKit框架的使用MapKit框架使用前提导入框架(Xcode5

iOS地图----MapKit框架

1.MapKit框架使用前提 ①导入框架 ②导入主头文件 #import <MapKit/MapKit.h> ③MapKit框架使用须知 MapKit框架中所有数据类型的前缀都是MK MapKit有一个比较重要的UI控件,专门用于地图显示 ④MapKit框架包含CLLocation 2.设置地图的类型 可以通过设置MKMapView的mapType设置地图类型 typedef enum : NSUInteger { MKMapTypeStandard , 标准(默认) MKMapTypeSat

百度地图API绘制带头箭头的折线

源代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> body, html,#allmap {width: 100%;height: 100%;overflow: hidden;marg

百度地图多边形绘制之多边形预判

js文件:https://pan.baidu.com/s/1hsIR8Ni 百度地图提供了多边形绘制的功能,但是对于不符合多边形规则的图形却没有做限制:   所以我们在绘制的时候需要判断当前绘制的图形是否是多边形: 1:下载百度地图开源库里面的鼠标绘制工具条库:DrawingManager.js 2:找到DrawingManager.prototype._bindPolylineOrPolygon方法里面的startAction方法,在我们绘制的时候会触发该方法,在startAction里我们会

Python调用百度地图API(路线规划、POI检索)——第一部分

项目的目的是为了查询某个点附近某些POI的最近距离,例如查询天安门到附近最近的商场的距离(时间) 1.百度地图API 程序中用到百度地图的两个API,分别是 (1)路线规划服务(又名Direction API):http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2 (2)地点检索服务(又名Place API):http://lbsyun.baidu.com/index.php?title=webapi/guide/webs