热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将自定义注释添加到MKMapview

如何解决《将自定义注释添加到MKMapview》经验,为你挑选了1个好方法。

我正在尝试添加注释 MKMapView

我创建了一个CustomMapPin符合MKAnnotation协议的类

#import 
#import 

@interface CustomMapPin : NSObject  {

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@property(nonatomic, strong) NSString *type; // this is to differentiate between the different annotations on the map

@end

我创建了一个类CustomMapAnnotationView,它是一个子类MKAnnotationView

CustomMapAnnotationView.h

#import 
#import 

@interface CustomMapAnnotationView : MKAnnotationView
@property (nonatomic, strong) UIImageView *annotationImage;
@end

CustomMapAnnotationView.m

#import "CustomMapAnnotationView.h"

@implementation CustomMapAnnotationView

-(id) initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    if (self) {
        self.frame = CGRectMake(0, 0, 28, 40);
        self.backgroundColor = [UIColor clearColor];

        self.annotatiOnImage= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 28, 40)];
        self.annotationImage.cOntentMode= UIViewContentModeScaleAspectFit;
        [self addSubview:self.annotationImage];

    }
    return self;
}

@end

我正在添加自定义引脚,FindMechanicViewController其中包含CLLocationManagerDelegateMKMapViewDelegate

代码段是:

-(void) viewWillAppear:(BOOL)animated {
    [self.locationManager startUpdatingLocation];
    self.currentLocation = [self.locationManager location];

    // Set the region of the map
    MKCoordinateSpan mapViewSpan = MKCoordinateSpanMake(0.01, 0.01);
    MKCoordinateRegion mapRegion = MKCoordinateRegionMake(self.currentLocation.coordinate, mapViewSpan);
    [self.mapView setRegion:mapRegion];

    // Add custom pin showing user location
    CustomMapPin *annotation = [[CustomMapPin alloc] init];
    annotation.title = @"Current Location";
    annotation.coordinate = self.currentLocation.coordinate;
    annotation.type = @"user location";
    [self.mapView addAnnotation:annotation];
}

和委托方法

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    static NSString *reuseId = @"CustomMapPin";
    CustomMapAnnotationView *annotatiOnView= (CustomMapAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

    if ([annotation isKindOfClass:[CustomMapPin class]]) {
        CustomMapPin *customAnnotation = (CustomMapPin *)annotation;

       if ([customAnnotation.type isEqualToString:@"user location"]) {
           [annotationView setAnnotation:customAnnotation];
           [annotationView setImage:[UIImage imageNamed:@"pin_user"]];
           [annotationView setCanShowCallout:YES];
        }

    }
    return annotationView;
}

这不会在地图上显示任何内容.我该如何解决 ?



1> 小智..:

viewForAnnotation,CustomMapAnnotationView从来没有实际上alloc + inited(dequeueReusableAnnotationViewWithIdentifier不这样做).

如果viewForAnnotation甚至被调用,它必须返回nil,这意味着地图视图必须在某处放置一个红色引脚(如果没有调用委托方法,那么地图视图将默认为红色引脚).记录添加注释的坐标并查看.

更正viewForAnnotation可能如下所示:

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    if ([annotation isKindOfClass:[CustomMapPin class]]) {
        static NSString *reuseId = @"CustomMapPin";

        CustomMapAnnotationView *annotatiOnView= (CustomMapAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if (!annotationView) {
            annotatiOnView= [[CustomMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
            [annotationView setCanShowCallout:YES];
        }

        CustomMapPin *customAnnotation = (CustomMapPin *)annotation;

        //view's annotation should be set regardless of "type"
        [annotationView setAnnotation:customAnnotation];

        if ([customAnnotation.type isEqualToString:@"user location"]) {
            [annotationView setImage:[UIImage imageNamed:@"pin_user"]];
        }
        else {
            //If it's not a "user location", then set image
            //to something else otherwise image will either be nil
            //or it will show some other annotation's image...
            [annotationView setImage:[UIImage imageNamed:@"pin_other"]];
        }

        return annotationView;
    }

    return nil;
}

代码的其他一些问题:

    viewWillAppear,代码正在调用后立即检索位置startUpdatingLocation.这并不能保证每次都能正常工作,即使它"有效",您很可能会得到一个旧的缓存位置.如果它不起作用,注释将最终在0,0(大西洋)或app将因无效坐标而崩溃.在didUpdateLocations委托方法中读取位置要好得多.在viewWillAppear,调用startUpdatingLocation然后在didUpdateLocations,如果位置的准确性和年龄对您来说足够,请调用stopUpdatingLocation然后使用该位置(创建并添加注释等).

    CustomMapAnnotationView,annotationImage创建并添加对象但image从未设置其属性.该annotationImage实际上从来没有真正用于任何东西.

    整个CustomMapAnnotationView类是不需要你的目的.在viewForAnnotation,代码是设置image属性CustomMapAnnotationView(而不是使用annotationImage).此image属性继承自MKAnnotationView.内置的基础MKAnnotationView课程就是您所需要的一切.它已经有一个image属性,它会自动为您显示它(您不需要创建自己的属性UIImageView).


推荐阅读
author-avatar
邹飞邹飞邹飞
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有