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;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM