在iOS地图上绘制两点间路线
2016-01-08 17:51
671 查看
http://blog.csdn.net/jasonblog/article/details/7785352
当我们获取了一组地理位置后,可能会想要在地图上绘制这组地理位置信息所包含的路线。
MKMapView提供了addOverlay功能(以及addAnnotation),让我们可以在地图上放一层遮罩。如果要放一组遮罩,可以用addOverlays。
[cpp] view
plaincopy
#pragma mark -
- (void)drawLineWithLocationArray:(NSArray *)locationArray
{
int pointCount = [locationArray count];
CLLocationCoordinate2D *coordinateArray = (CLLocationCoordinate2D *)malloc(pointCount * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < pointCount; ++i) {
CLLocation *location = [locationArray objectAtIndex:i];
coordinateArray[i] = [location coordinate];
}
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]];
[self.mapView addOverlay:self.routeLine];
free(coordinateArray);
coordinateArray = NULL;
}
MKPolyLine为我们提供了方便绘制多条线段的功能,它实现了MKOverlay协议,但并不能作为遮罩。我们需要实现相应的遮罩代理方法:
[cpp] view
plaincopy
#pragma mark - MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if(overlay == self.routeLine) {
if(nil == self.routeLineView) {
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 5;
}
return self.routeLineView;
}
return nil;
}
下面是我的测试代码,用北京的经纬度和杭州的经纬度画线:
[cpp] view
plaincopy
- (void)drawTestLine
{
CLLocation *location0 = [[CLLocation alloc] initWithLatitude:39.954245 longitude:116.312455];
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:30.247871 longitude:120.127683];
NSArray *array = [NSArray arrayWithObjects:location0, location1, nil];
[self drawLineWithLocationArray:array];
}
绘制结果如下:
![](http://my.csdn.net/uploads/201207/25/1343205596_9055.png)
原文地址:http://blog.csdn.net/jasonblog/article/details/7785352
Jason Lee @ Hangzhou
当我们获取了一组地理位置后,可能会想要在地图上绘制这组地理位置信息所包含的路线。
MKMapView提供了addOverlay功能(以及addAnnotation),让我们可以在地图上放一层遮罩。如果要放一组遮罩,可以用addOverlays。
[cpp] view
plaincopy
#pragma mark -
- (void)drawLineWithLocationArray:(NSArray *)locationArray
{
int pointCount = [locationArray count];
CLLocationCoordinate2D *coordinateArray = (CLLocationCoordinate2D *)malloc(pointCount * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < pointCount; ++i) {
CLLocation *location = [locationArray objectAtIndex:i];
coordinateArray[i] = [location coordinate];
}
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]];
[self.mapView addOverlay:self.routeLine];
free(coordinateArray);
coordinateArray = NULL;
}
MKPolyLine为我们提供了方便绘制多条线段的功能,它实现了MKOverlay协议,但并不能作为遮罩。我们需要实现相应的遮罩代理方法:
[cpp] view
plaincopy
#pragma mark - MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if(overlay == self.routeLine) {
if(nil == self.routeLineView) {
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 5;
}
return self.routeLineView;
}
return nil;
}
下面是我的测试代码,用北京的经纬度和杭州的经纬度画线:
[cpp] view
plaincopy
- (void)drawTestLine
{
CLLocation *location0 = [[CLLocation alloc] initWithLatitude:39.954245 longitude:116.312455];
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:30.247871 longitude:120.127683];
NSArray *array = [NSArray arrayWithObjects:location0, location1, nil];
[self drawLineWithLocationArray:array];
}
绘制结果如下:
![](http://my.csdn.net/uploads/201207/25/1343205596_9055.png)
原文地址:http://blog.csdn.net/jasonblog/article/details/7785352
Jason Lee @ Hangzhou
相关文章推荐
- iOS 实现渐变色
- 把删除数组里面字典文件,然后进行倒叙,删除指定的数组元素ios考核第四题
- ios 调用系统提示音教程
- 3种隐藏键盘的方法——iOS
- ios enum to string
- ios 获取文件夹大小
- 原创翻译:蓝牙(BLE)for iOS
- iOS-设置启动页面时间
- iOS 7
- iOS- 如何集成支付宝
- 【iOS】倒计时5分钟
- CLLocationManager Delegate methods are not getting called -- ios8 later
- iOS 列表页跳转到详细页 详细页隐藏TabBar
- iOS 5
- ios实现长时间后台运行
- iOS 6
- ios--随笔一
- iOS模拟器键盘输入中文解决方案
- ios4 习题
- iOS 第七期考核题(字符串以及字典的使用 数组内容转换成字符串)