iOS高德地图自定义annotation添加不同图片

1.model类里面添加index

#import <MAMapKit/MAMapKit.h>

#import <AMapSearchKit/AMapCommonObj.h>

 

@interface POIAnnotation : NSObject <MAAnnotation>

 

- (id)initWithPOI:(AMapPOI *)poi;

 

@property (nonatomic, readonly, strong) AMapPOI *poi;

@property(assign,nonatomic)NSInteger idx;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

 

/*!

 @brief 获取annotation标题

 @return 返回annotation的标题信息

 */

- (NSString *)title;

 

/*!

 @brief 获取annotation副标题

 @return 返回annotation的副标题信息

 */

- (NSString *)subtitle;

/*!

 @brief 获取经纬度

 @return 返回annotation的经纬度

 */

- (AMapGeoPoint *)location;

@end

2.给index赋值

/* POI 搜索回调. */

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response

{

    if (response.pois.count == 0)

    {

        return;

    }

    NSMutableArray *poiAnnotations = [NSMutableArray arrayWithCapacity:response.pois.count];

    [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {

    POIAnnotation *annotation =[[POIAnnotation alloc] initWithPOI:obj];

    annotation.idx = idx;

    [poiAnnotations addObject:annotation];

    }];

    /* 将结果以annotation的形式加载到地图上. */

    [self.mapView addAnnotations:poiAnnotations];

    /* 如果只有一个结果,设置其为中心点. */

    if (poiAnnotations.count == 1)

    {

        [self.mapView setCenterCoordinate:[poiAnnotations[0] coordinate]];

    }

    /* 如果有多个结果, 设置地图使所有的annotation都可见. */

    else

    {

        [self.mapView showAnnotations:poiAnnotations animated:NO];

    }

}

3.取值

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation

{

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

    {

 

 

        static NSString *poiIdentifier = @"poiIdentifier";

        MAPinAnnotationView *poiAnnotationView = (MAPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:poiIdentifier];

        if (poiAnnotationView == nil)

        {

            poiAnnotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:poiIdentifier];

        }

        poiAnnotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"poi_marker_%ld.png",((POIAnnotation*)annotation).idx]];

 

        poiAnnotationView.canShowCallout = NO;

        return poiAnnotationView;

    }

        return nil;

}

 

转载于:https://www.cnblogs.com/liuting-1204/p/5628111.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值