2020-01-11 IOS原生地圖開發(fā)(3)——csv解析與自定義標(biāo)記Annotation

上一篇介紹了加載保存離線Google衛(wèi)星瓦片的方法,其主要原因是在高縮放級別下,高德百度地圖是沒有數(shù)據(jù)的,而Google衛(wèi)星有數(shù)據(jù)。
本篇將介紹如何解析包含颶風(fēng)軌跡的CSV文件并自定義標(biāo)記進(jìn)行顯示。
本篇內(nèi)容:CSV解析 自定義標(biāo)記顯示

先看效果:
颶風(fēng)1的運(yùn)行軌跡

颶風(fēng)2的運(yùn)行軌跡

一、包含空間數(shù)據(jù)的CSV解析

1、颶風(fēng)1csv數(shù)據(jù)的結(jié)構(gòu)


颶風(fēng)1csv結(jié)構(gòu)

2、解讀csv得到包含每一行數(shù)據(jù)字符串的數(shù)組

//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];
//取出每一行的數(shù)據(jù)
NSArray *_allLinedStrings = [fileContents componentsSeparatedByString:@"\r\n"];
NSLog(@"%@",_allLinedStrings);
    return _allLinedStrings;
    
}

3、按空格分解可得到坐標(biāo)和每個(gè)屬性的值 保存到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的實(shí)現(xiàn)
1、構(gòu)造自定義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]];//不同的顏色代表不同的颶風(fēng)
    }
    
    return self;
}

@end

2、實(shí)現(xiàn)自定義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、實(shí)現(xiàn)委托方法(很重要)

- (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;
}

三、方法調(diào)用

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];
        }
        
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容