iOS之使用MapKit通过经纬度坐标画线
2015-11-20 16:19
483 查看
在使用iOS sdk自带的mapkit 需要引入mapkit头文件
首先定义几个全局的变量,包含map视图、线及线视图,并包含map的委托
初始化mapView
通过经纬度坐标画线及添加大头针
实现 mapView delegate的两个方法
下面写一个测试的方法:
最终的效果图如下:
#import <MapKit/MapKit.h>
首先定义几个全局的变量,包含map视图、线及线视图,并包含map的委托
@property (retain, nonatomic) MKPolyline* routeLine; @property (retain, nonatomic) MKMapView* mapView; @property (retain, nonatomic) MKPolylineView* routeLineView;
初始化mapView
MKMapView* mapView = [[MKMapView alloc]initWithFrame:self.view.bounds]; mapView.delegate = self; [self.view addSubview:mapView]; self.mapView = mapView;
通过经纬度坐标画线及添加大头针
- (void)drawLineWithLocationArray:(NSArray *)locationArray { NSInteger 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]; // 添加大头针 MKPointAnnotation *ann = [[MKPointAnnotation alloc] init]; ann.coordinate = [location coordinate]; [ann setTitle:[NSString stringWithFormat:@"%i",i]]; [ann setSubtitle:@""]; [_mapView addAnnotation:ann]; } self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount]; [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; [self.mapView addOverlay:self.routeLine]; free(coordinateArray); coordinateArray = NULL; }
实现 mapView delegate的两个方法
// 画线的委托实现方法 - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay { if(overlay == self.routeLine) { if(nil == self.routeLineView) { self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; self.routeLineView.fillColor = [UIColor blueColor]; self.routeLineView.strokeColor = [UIColor redColor]; self.routeLineView.lineWidth = 5; } return self.routeLineView; } return nil; } // 添加大头针的委托实现方法 - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation { MKPinAnnotationView *pinView = nil; static NSString *defaultPinID = @"pinId"; pinView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID]; pinView.pinTintColor = [UIColor redColor]; pinView.canShowCallout = YES; pinView.animatesDrop = YES; return pinView; }
下面写一个测试的方法:
- (void)drawLine { // 如果存在线,移除 if (self.mapView.annotations.count > 0) { [self.mapView removeAnnotations:_mapView.annotations]; [self.mapView removeOverlays:_mapView.overlays]; return; } // 模拟经纬度坐标点 CLLocation *location0 = [[CLLocation alloc] initWithLatitude:39.954245 longitude:116.312455]; CLLocation *location1 = [[CLLocation alloc] initWithLatitude:38.954245 longitude:116.512455]; CLLocation *location2 = [[CLLocation alloc] initWithLatitude:37.954245 longitude:116.612455]; CLLocation *location3 = [[CLLocation alloc] initWithLatitude:36.954245 longitude:116.712455]; CLLocation *location4 = [[CLLocation alloc] initWithLatitude:35.954245 longitude:116.812455]; CLLocation *location5 = [[CLLocation alloc] initWithLatitude:34.954245 longitude:117.312455]; CLLocation *location6 = [[CLLocation alloc] initWithLatitude:33.954245 longitude:118.312455]; CLLocation *location7 = [[CLLocation alloc] initWithLatitude:32.954245 longitude:119.312455]; CLLocation *location8 = [[CLLocation alloc] initWithLatitude:31.954245 longitude:119.612455]; CLLocation *location9 = [[CLLocation alloc] initWithLatitude:30.247871 longitude:120.127683]; NSArray *array = [NSArray arrayWithObjects:location0, location1,location2,location3,location4,location5,location6,location7,location8,location9, nil]; [self drawLineWithLocationArray:array]; }
最终的效果图如下:
相关文章推荐
- ios应用启动时上下有黑边
- IOS学习——Navigation Controller
- iOS中GCD的使用小结
- iOS新加速计事件
- iOS笔记--oc语法3 (#include和#import的区别、#import和@class的区别)
- IOS开发者常用的10个Xcode插件
- 在IOS应用中从竖屏模式强制转换为横屏模式(手动转换)
- ios 个人苹果开发者账号申请
- IOS 公司开发者账号申请详细教程
- ios 中 set get 方法 便利构造器的创建 Block 动画
- IOS中图形图像处理第一部分:位图图像原图修改
- iOS 9 spotlight搜索 3DTouch
- iOS 时间格式转化合集
- IOS开发过程中的变量命名规范和图片及切图命名规范
- iOS深入学习(使用CocoaPods进行第三方资源管理)
- iOS NSDecimalNumber 货币计算 四舍五入
- iOS CocoaPods安装和使用图解
- iOS移动开发CoreDate讲解
- IOS沙盒机制(SandBox)
- iOS 音乐 锁屏显示 控制