IOS高德地图自定制多种不同类型的大头针

2017-03-02  本文已影响720人  plu

根据项目需求,需要自定制起点、途经点以及终点的大头针,下面看一下怎么去创建?

1.自定义MyPointAnnotation类继承MAPointAnnotation,在此基础上添加属性,系统中的MAPointAnnotation属性太少,只能自己去添加。

#import <MAMapKit/MAMapKit.h>

@interface MyPointAnnition : MAPointAnnotation

@property(nonatomic,copy)NSString*siteNumber;//大头针的区分符号

@end

2.当然了,别忘记在CustomAnnotationView中设置对应的siteNumber属性哦

3.1️⃣在创建大头针的时候:

MyPointAnnition *pointAnnotation = [[MyPointAnnition alloc]init];

pointAnnotation.siteNumber=[NSString stringWithFormat:@"%d",i];//给siteNumber赋值

pointAnnotation.coordinate = CLLocationCoordinate2DMake(randomCoordinate.latitude, randomCoordinate.longitude);

[_mapView addAnnotation:pointAnnotation];

2️⃣在大头针的代理方法中:

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation

{

if ([annotation isKindOfClass:[MyPointAnnition class]])

{

MyPointAnnition *ano=annotation;

static NSString *reuseIndetifier = @"annotationReuseIndetifier";

CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

if (annotationView == nil)

{

annotationView = [[CustomAnnotationView alloc] initWithAnnotation:ano

reuseIdentifier:reuseIndetifier];

}

annotationView.canShowCallout= YES;      //设置气泡可以弹出,默认为NO

annotationView.siteNumber=ano.siteNumber;

annotationView.centerOffset = CGPointMake(0, 0);

if ([ano.siteNumber integerValue]==1)

{

annotationView.image = [UIImage imageNamed:@"animatedCar_1"];

}

else if ([ano.siteNumber integerValue]==2)

{

annotationView.image = [UIImage imageNamed:@"animatedTrain_1"];

}else if([ano.siteNumber integerValue]==3)

{

annotationView.image = [UIImage imageNamed:@"Starting Point"];

}else{

annotationView.image = [UIImage imageNamed:@"hema"];

}

//        annotationView.portrait = [UIImage imageNamed:@"hema"];

return annotationView;

}

return nil;

}

上一篇下一篇

猜你喜欢

热点阅读