`
jsntghf
  • 浏览: 2511501 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

地图描线的简单示例

    博客分类:
  • iOS
阅读更多

首先,需要将CoreLocation.framework和MapKit.framework加入到项目中。

 

 

mapLinesViewController.h

 

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MapRouteLayerView.h"

@interface mapLinesViewController : UIViewController {
	MKMapView *mapView;
	MapRouteLayerView *routeView;
}

@property(nonatomic, retain) MKMapView *mapView;
@property(nonatomic, retain) MapRouteLayerView *routeView;

@end

 

mapLinesViewController.m

 

#import "mapLinesViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "MapRouteLayerView.h"

@implementation mapLinesViewController

@synthesize routeView;
@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];
	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"route" ofType:@"csv"];
	NSString *fileContents = [NSString stringWithContentsOfFile:filePath];
	NSArray *pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];	
	NSMutableArray *points = [[NSMutableArray alloc] initWithCapacity:pointStrings.count];	
	for(int idx = 0; idx < pointStrings.count; idx++) {
		NSString *currentPointString = [pointStrings objectAtIndex:idx];
		NSArray *latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];		
		CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
		CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];		
		CLLocation *currentLocation = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
		[points addObject:currentLocation];
	}
	mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
	[self.view addSubview:mapView];
	routeView = [[MapRouteLayerView alloc] initWithRoute:points mapView:mapView];	
	[points release];
}

- (void)viewDidUnload {
	self.mapView = nil;
	self.routeView = nil;
}

- (void)dealloc {	
	[mapView release];
	[routeView release];
    [super dealloc];
}

@end

 

MapRouteLayerView.h

 

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapRouteLayerView : UIView <MKMapViewDelegate> {
	MKMapView *mapView;
	NSArray *points;
	UIColor *lineColor;
}

@property(nonatomic, retain) NSArray *points;
@property(nonatomic, retain) MKMapView *mapView;
@property(nonatomic, retain) UIColor *lineColor; 

- (id)initWithRoute:(NSArray *)routePoints mapView:(MKMapView *)mapView;

@end

 

MapRouteLayerView.m

 

#import "MapRouteLayerView.h"

@implementation MapRouteLayerView

@synthesize mapView;
@synthesize points;
@synthesize lineColor; 

- (id)initWithRoute:(NSArray *)routePoints mapView:(MKMapView *)mapView {
	self = [super initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
	[self setBackgroundColor:[UIColor clearColor]];	
	[self setMapView:mapView];
	[self setPoints:routePoints];
	
	CLLocationDegrees maxLat = -90;
	CLLocationDegrees maxLon = -180;
	CLLocationDegrees minLat = 90;
	CLLocationDegrees minLon = 180;
	
	for(int idx = 0; idx < self.points.count; idx++) {
		CLLocation *currentLocation = [self.points objectAtIndex:idx];
		if(currentLocation.coordinate.latitude > maxLat)
			maxLat = currentLocation.coordinate.latitude;
		if(currentLocation.coordinate.latitude < minLat)
			minLat = currentLocation.coordinate.latitude;
		if(currentLocation.coordinate.longitude > maxLon)
			maxLon = currentLocation.coordinate.longitude;
		if(currentLocation.coordinate.longitude < minLon)
			minLon = currentLocation.coordinate.longitude;
	}
	
	MKCoordinateRegion region;
	region.center.latitude = (maxLat + minLat) / 2;
	region.center.longitude = (maxLon + minLon) / 2;
	region.span.latitudeDelta = maxLat - minLat;
	region.span.longitudeDelta = maxLon - minLon;
	
	[self.mapView setRegion:region];
	[self.mapView setDelegate:self];
	[self.mapView addSubview:self];
	
	return self;
}

- (void)drawRect:(CGRect)rect {
	if(!self.hidden && nil != self.points && self.points.count > 0) {
		CGContextRef context = UIGraphicsGetCurrentContext(); 		
		if(nil == self.lineColor)
			self.lineColor = [UIColor blueColor];		
		CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
		CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
		CGContextSetLineWidth(context, 2.0);		
		for(int idx = 0; idx < self.points.count; idx++) {
			CLLocation *location = [self.points objectAtIndex:idx];
			CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:self];			
			if(idx == 0) {
				CGContextMoveToPoint(context, point.x, point.y);
			} else {
				CGContextAddLineToPoint(context, point.x, point.y);
			}
		}		
		CGContextStrokePath(context);
	}
}

#pragma mark mapView delegate functions
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
	self.hidden = YES;
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
	self.hidden = NO;
	[self setNeedsDisplay];
}

- (void) dealloc {
	[points release];
	[mapView release];
	[lineColor release];
	[super dealloc];
}

@end

 

示例图:


分享到:
评论

相关推荐

    地图描线最新版

    地图描线最新版是一款专为地图制图爱好者和专业人员设计的应用程序,它提供了高效、精确的工具来在地图上绘制线条和路径。这个“最新版”可能包含了若干改进和新功能,旨在提升用户体验和工作效率。 在地图描线的...

    图形学(描线填充)

    本主题将深入探讨“描线填充”这一概念,它在计算机图形学中占据了核心地位,特别是在2D图形渲染中。我们将使用C++编程语言和Microsoft Foundation Classes (MFC)库来实现这个功能。 描线填充是计算机图形学中的...

    Photoshop描线效果操作技巧图解教程.doc

    Photoshop描线效果操作技巧图解教程

    浮雕应用实例教程(描线).pdf

    描线时尽量贴近对象边缘,并注意软件的自动连接功能,它可以自动连接断开的线条,形成连续的曲面或封闭曲线。对于复杂的形体,可以用不同颜色的线条描绘内部结构,以便在浮雕模块中更清晰地理解高度走向。 3. 新建...

    OpenCV实例程序源代码-霍夫变换描线.c

    OpenCV实例程序源代码-霍夫变换描线.c

    [矢量素材]日本矢量描线[花.草.动物.底纹].zip

    [矢量素材]日本矢量描线[花.草.动物.底纹].zip[矢量素材]日本矢量描线[花.草.动物.底纹].zip

    Windows 11 printed document-1.pdf 平板对照描线 防止漏接线

    【标题】"Windows 11 printed document-1.pdf 平板对照描线 防止漏接线" 提供的是一份与电子工程相关的技术文档,可能是关于电路设计或电子课设的作业。该文档可能包含了一种使用平板电脑进行电路连线检查的方法,以...

    OpenCV3编程学习笔记+OpenCV实例程序源代码(20个).zip

    20170425_霍夫变换描线.c 20170426_仿射变换.c 20170426_按键实现重映射变换.c 20170426_滑条控制轮廓检测及绘制.c 20170426_简单的图像颠倒程序.c 20170426_鼠标拖动图像修复.c 20170427_凸包生成.c 20170427_用...

    宝贝涂鸦(幼儿涂鸦软件)v2.0.1中文绿色免费版

    宝贝涂鸦软件是一款一个适合3岁以上孩子使用的涂鸦程序,功能相对简单,但是内容不算少,还有20余张精美图片可供涂色。 其他的入如章等功能一个不少。总之,Drawing 4 Kids作为来自于国外的一个小软件,是一个小而且...

    EasyPaintTool SAIv2.0

    主要用前期草稿、描线以及上色阶段。目前数码绘行业内比较为人熟知的各种画风都能使用SAI来绘制,比如赛璐璐、厚涂、半厚涂比较典型的上色风格。 作者:动漫绘画百科 https://www.bilibili.com/read/cv611499/ 出处...

    flash短片《善良的死神》毕设论文论文.doc

    3. FLASH 描线:FLASH 描线是 Flash 软件中的一种重要功能,通过描线可以实现动画的基本形状和结构,描线技术对动画效果的影响非常大。 4. 动作调节:动作调节是 Flash 软件中的一种重要功能,通过动作调节可以实现...

    程博士幼童智能开发教程.手眼协调训练

    《手眼协调训练》包括画点训练、点连线训练、描线训练、连线训练、仿线训练、画线训练、综合训练、走迷宫训练、简单涂色训练、分块涂色训练、走迷宫涂色训练、情景涂色训练等方面的内容,每项内容均遵循多角度、小...

    二维动画制作流程PPT课件.pptx

    "二维动画制作流程PPT课件" 本资源为一份关于二维动画制作流程的PPT...该资源为二维动画制作流程的详细介绍,涵盖了从剧本设计到影片完成的各个阶段,提供了详细的解释和示例,非常适合动画爱好者和从业者学习和参考。

    SAI绘画简单教程:勾线、上色等.docx

    - 创建一个新的钢笔图层,然后选择曲线工具进行描线。你可以自由地添加描点,SAI会自动平滑线条,使其看起来更加流畅。如果你有绘图板,可以直接在草稿图层上新建一层勾线,降低草稿图层的透明度以辅助画线,这样更...

    使用ARCSCAN

    完成线要素后,可以按F2键结束描线,生成线要素。 **练习2:批量矢量化** 在这一部分,用户将学习编辑栅格图层,移除不必要的像元,应用矢量化设置,预览矢量化结果,并使用批量矢量化工具。批量矢量化允许用户一次...

    印刷电路板知识介绍.pptx

    描线可以用油漆描线或用指甲油描线。腐蚀可以用三绿化铁腐蚀液或双氧水 + 盐酸腐蚀液。钻孔时,需要先用定位钉定位,然后钻孔,注意钻孔前,一定要先用定位钉定位,以免钻孔时钻头打滑,将孔钻偏或将钻头折断。 ...

    新北师大二上折一折做一做PPT课件.pptx

    在这个过程中,他们可以模仿课件中的示例,或者尝试简单的剪纸练习,从而熟悉剪纸的基本手法。 第四部分,“想一想说一说”,引导学生思考剪纸过程中可能遇到的问题,如操作技巧、安全注意事项等,并鼓励他们相互...

    opencv3_学习笔记+自己整理的程序小代码.zip

    20170425_霍夫变换描线.c 20170426_仿射变换.c 20170426_按键实现重映射变换.c 20170426_滑条控制轮廓检测及绘制.c 20170426_简单的图像颠倒程序.c 20170426_鼠标拖动图像修复.c 20170427_凸包生成.c 20170427_用...

    sai绘画的整个步骤.doc

    如果只是简单涂鸦,可以选择默认的72分辨率。 - 分辨率的设定会影响画作的清晰度和细节表现,高分辨率适合精细绘制,低分辨率则适用于快速草图。 2. **绘制草稿**: - 在这一步,画家应快速将自己的创意和构思...

Global site tag (gtag.js) - Google Analytics