IOS:将图像添加到自定义MKAnnotationview [英] IOS: Adding image to custom MKAnnotationview

查看:114
本文介绍了IOS:将图像添加到自定义MKAnnotationview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将自定义图片添加到地图中的注释中。我做了以下自定义MapAnnotationView:

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

@interface MapAnnotation:MKAnnotationView< MKAnnotation>

@property(非原子)CGFloat lat;
@property(非原子)CGFloat lon;
@property(非原子)CGFloat高度;
@property(非原子,复制)NSString * title;
@property(非原子,复制)NSString *副标题;
@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)坐标; {
CLLocationCoordinate2D position;
if(_lat!= 0.0&& _lon!= 0.0){
position.latitude = _lat;
position.longitude = _lon;

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

返回头寸;
}

@end

- (void)mapDataToMapAnnotations {

NSMutableArray * toRemove = [NSMutableArray arrayWithCapacity:10];
(_map.annotations中的id注释)
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 ]在里面];

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 ....我确信图标不为空,已经检查过。



谢谢

mapView:viewForAnnotation:。

   - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id< MKAnnotation>)注释
{
静态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 =注解;

返回annotationView;
}

为了封装更多,您应该像创建自定义注释视图一样创建并提供我们建议你重命名 MapAnnotation 类,因为它很混乱。 iOS中还有Annotations是这些注释视图的数据持有者。为了解决这个问题,我宁愿编写继承类的类型,在这个例子中,在你的自定义类的最后有 MKAnnotationView 。例如 CustomPinAnnotationView


I want to add a custom image to my annotations in the map. And i have made the following custom 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];
        }
    }
}

The problem is that the pins is the standard redPin.... I am sure that the icons isn't null, have checked for that.

Thanks

解决方案

You have to serve the MapKit delegate method mapView:viewForAnnotation: with a custom view.

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

To encapsulate more you should create a custom annotation view like you did and serve the delegate method above with your class.

I advise you to rename the MapAnnotation class because it is confusing. There are also Annotations in iOS which are the data holders for those annotation views. To solve this I would prefer to write the type of the inherited class, in this case MKAnnotationView at the end of your custom class. For example CustomPinAnnotationView.

这篇关于IOS:将图像添加到自定义MKAnnotationview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆