上一篇介绍了加载保存离线Google卫星瓦片的方法,其主要原因是在高缩放级别下,高德百度地图是没有数据的,而Google卫星有数据。
本篇将介绍如何解析包含飓风轨迹的CSV文件并自定义标记进行显示。
本篇内容:CSV解析 自定义标记显示
飓风2的运行轨迹
一、包含空间数据的CSV解析
1、飓风1csv数据的结构
飓风1csv结构
2、解读csv得到包含每一行数据字符串的数组
//mmethod for csv files read
-(NSArray *)readCSVData:(NSString*) nameStr{
NSString *path = [[NSBundle mainBundle] pathForResource:nameStr ofType:@"csv"];
NSError *error = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
//取出每一行的数据
NSArray *_allLinedStrings = [fileContents componentsSeparatedByString:@"\r\n"];
NSLog(@"%@",_allLinedStrings);
return _allLinedStrings;
}
3、按空格分解可得到坐标和每个属性的值 保存到Hurricane对象。
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface Hurricane : NSObject
@property(nonatomic, readonly) NSString * date;
@property(nonatomic, readonly) NSString * time;
@property(nonatomic, readonly) NSString * wind;
@property(nonatomic, readonly) NSString * presure;
@property(nonatomic, readonly) NSString * stormType;
@property(nonatomic, readonly) NSString * category;
@property(nonatomic, readonly) NSString * name;
@property(nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id) initWithProperStr:(NSString *)ProperStr ;
@end
NS_ASSUME_NONNULL_END
#import "Hurricane.h"
@implementation Hurricane
-(id) initWithProperStr:(NSString *)ProperStr{
self = [super init];
if (self && ![ProperStr isEqualToString:@""]) {
NSArray *array = [ProperStr componentsSeparatedByString:@","];
//set the propers
_date = array[0];
_time = array[1];
if (array[2]&&array[3]) {
double lat = [array[2] doubleValue];
double lon = [array[3] doubleValue];
_coordinate = CLLocationCoordinate2DMake(lat, lon);
}
_wind = array[4];
_presure = array[5];
_stormType = array[6];
_category = array[7];
_name = array[8];
}
return self;
}
@end
二、自定义Annotation的实现
1、构造自定义AnnotationView
#import <MapKit/MapKit.h>
@interface PointAnnotationView : MKAnnotationView
- (id)initWithAnnotationColor:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier colorNum:(int)colorNum;
@end
#import "PointAnnotationView.h"
@implementation PointAnnotationView
- (id)initWithAnnotationColor:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier colorNum:(int)colorNum
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.enabled = YES;
self.draggable = NO;
self.image = [UIImage imageNamed:[NSString stringWithFormat:@"circle_%d",colorNum]];//不同的颜色代表不同的飓风
}
return self;
}
@end
2、实现自定义Annotation
#import <Foundation/Foundation.h>
#import "PointAnnotationView.h"
#import "Hurricane.h"
NS_ASSUME_NONNULL_BEGIN
@interface PointAnnotation : NSObject<MKAnnotation>
@property(nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property(nonatomic, strong) Hurricane * hurricane;
//colorNum used for color chioce
@property(nonatomic,assign) int colorNum;
@property(nonatomic, weak) PointAnnotationView* annotationView;
-(id) initWithHurricane:(Hurricane *)hurricane colorNum:(int) colorNum;
@end
NS_ASSUME_NONNULL_END
#import "PointAnnotation.h"
@implementation PointAnnotation
-(id)initWithHurricane:(Hurricane *)hurricane colorNum:(int)colorNum{
self = [super init];
if (self) {
if (hurricane) {
_hurricane = [[Hurricane alloc] init];
_hurricane = hurricane;
}
_coordinate = _hurricane.coordinate;
_colorNum = colorNum;
}
return self;
}
@end
3、实现委托方法(很重要)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[PointAnnotation class]])
{
PointAnnotationView* annoView = [[PointAnnotationView alloc] initWithAnnotationColor:annotation reuseIdentifier:@"Point_Annotation" colorNum:((PointAnnotation *)annotation).colorNum ];
((PointAnnotation*)annotation).annotationView = annoView;
return annoView;
}
return nil;
}
三、方法调用
NSArray* properArray = [self readCSVData:csvUrl];
for (int i = 1; i<properArray.count; i++) {
NSString * proper = properArray[i];
//排除最后一行
if (![proper isEqualToString:@""]) {
Hurricane * hurricane = [[Hurricane alloc] initWithProperStr:proper];
PointAnnotation * ann = [[PointAnnotation alloc] initWithHurricane:hurricane colorNum:colorNUm];
[_mapView addAnnotation:ann];
}
}
网友评论