【问题标题】:IOS: Adding image to custom MKAnnotationviewIOS:将图像添加到自定义 MKAnnotationview
【发布时间】:2012-03-09 09:58:47
【问题描述】:

我想在地图中的注释中添加自定义图像。我已经制作了以下自定义 MapAnnotationView:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@class POI;

@interface MapAnnotation : MKAnnotationView <MKAnnotation >

@property (nonatomic) CGFloat lat; 
@property (nonatomic) CGFloat lon;
@property (nonatomic) CGFloat altitude; 
@property (nonatomic,  copy) NSString * title;
@property (nonatomic, copy) NSString * subtitle;
@property (nonatomic,retain) NSString *source;
@property (nonatomic,retain) UIImage  *image;

@end

@implementation MapAnnotation
@synthesize coordinate;
@synthesize lat=_lat,lon=_lon,altitude= _altitude;
@synthesize subtitle= _subtitle, title= _title, source=_source, image =_img;


- (CLLocationCoordinate2D)coordinate;{
    CLLocationCoordinate2D position;
    if (_lat != 0.0 && _lon != 0.0) {
        position.latitude = _lat;
        position.longitude = _lon;

    }else {
        position.latitude=0.0;
        position.longitude=0.0;
    }

    return position; 
}

@end

-(void) mapDataToMapAnnotations{

    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10];
    for (id annotation in _map.annotations)
        if (annotation != _map.userLocation)
            [toRemove addObject:annotation];
    [_map removeAnnotations:toRemove];

    [_data removeAllObjects];

    [_data addObjectsFromArray:[UDdelegate naturArray]];


    if(_data != nil){
        MapAnnotation * tmpPlace;
        //for(NSDictionary * poi in _data){


        for(POI* poi in _data){

            tmpPlace = [[MapAnnotation alloc]init];

            tmpPlace.title = [poi title];
            tmpPlace.lat = [poi lat];
            tmpPlace.lon = [poi lon];
            tmpPlace.subtitle = [poi dist];
            tmpPlace.image = [poi poiIcon];

            [self.map addAnnotation:tmpPlace];
            [_map setNeedsLayout];
        }
    }
}

问题是引脚是标准的 redPin.... 我确定图标不为空,已经检查过了。

谢谢

【问题讨论】:

    标签: ios geolocation mkmapview mkannotationview


    【解决方案1】:

    您必须为 MapKit 委托方法 mapView:viewForAnnotation: 提供自定义视图。

    - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
    {
        static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";
    
        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];
    
        if (annotationView == nil)
        {
            annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier] autorelease];
        }
    
        annotationView.image = [UIImage imageNamed:@"pin_image.png"];
        annotationView.annotation = annotation;
    
        return annotationView;
    }
    

    要封装更多内容,您应该像以前一样创建一个自定义注释视图,并将上面的委托方法与您的类一起提供。

    我建议您重命名 MapAnnotation 类,因为它会造成混淆。 iOS 中还有注释,它们是这些注释视图的数据持有者。为了解决这个问题,我宁愿写继承类的类型,在这种情况下 MKAnnotationView 在你的自定义类的末尾。例如CustomPinAnnotationView

    【讨论】:

    • 在我的情况下,上面的委托方法没有被调用,即使我添加了 委托。 (也将其分配为 mapView.delegate=self;)
    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 2014-05-31
    • 2012-10-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多