向地图视图添加多个注释不起作用

发布于 2024-10-19 09:46:28 字数 4508 浏览 4 评论 0原文

因此,我已成功使用自定义图像向地图视图添加了单个注释,但是如果我添加第二个注释,它只会拒绝在地图上显示。

以下是相关代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    //mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate = self;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Location Info" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(showCurrentLocationButtonTapped:)];

    [self loadOverlays];

    //TEST LOAD TWO ANNOTATIONS
    //MKPointAnnotation *testAnnot = [[MKPointAnnotation alloc] init];
    ArboretumAnnotation *arboAnnot = [[ArboretumAnnotation alloc] init];
    CLLocationCoordinate2D workingCoord;
    workingCoord.latitude = /*some coord*/;
    workingCoord.longitude = /*some coord*/;
    [arboAnnot setCoordinate:workingCoord];
    [arboAnnot setTitle:@"Test Title"];
    [arboAnnot setSubtitle:@"Test Subtitle"];
    [mapView addAnnotation:arboAnnot];
    [arboAnnot release];

    ArboretumAnnotation *arboAnnot2 = [[ArboretumAnnotation alloc] init];
    CLLocationCoordinate2D workingCoord2;
    workingCoord2.latitude = /*some coord*/;
    workingCoord2.longitude = /*some coord*/;
    [arboAnnot2 setCoordinate:workingCoord2];
    [arboAnnot2 setTitle:@"Test Title2"];
    [arboAnnot2 setSubtitle:@"Test Subtitle2"];
    [mapView addAnnotation:arboAnnot2];
    [arboAnnot2 release];
//...
}
//...
- (ArboretumAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:MKUserLocation.class]) {
        //it's the built-in user location annotation(blue dot)
        return nil;
    }

    NSString *annotIdentifier = @"annotation";

    MKPinAnnotationView *recycledAnnotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotIdentifier];
    if (!recycledAnnotationView) {
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:annotIdentifier] autorelease];

        UIImage *iconImage = [UIImage imageNamed:@"arboretum.png"];

        CGRect resizeRect;

        resizeRect.size = iconImage.size;
        CGSize maxSize = CGRectInset(self.view.bounds,
                                     10.0f,
                                     10.0f).size;
        maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0f;
        if (resizeRect.size.width > maxSize.width)
            resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
        if (resizeRect.size.height > maxSize.height)
            resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

        customPinView.image = iconImage;
        //customPinView.image.frame = CGRectMake(kBorder, kBorder, kWidth - 2 * kBorder, kWidth - 2 * kBorder);
        customPinView.opaque = NO;
        //customPinView.pinColor = MKPinAnnotationColorPurple;
        customPinView.canShowCallout = YES;
        return customPinView;
    } else {
        recycledAnnotationView.annotation = annotation;
    }
    return recycledAnnotationView;

}

ArboretumAnnotation 标头:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface ArboretumAnnotation : NSObject <MKAnnotation> {
    NSString *title;
    NSString *subtitle;
    UIImage *image;

    CLLocationCoordinate2D coordinate;

}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) UIImage *image;

@property (nonatomic) CLLocationCoordinate2D coordinate;

@end

ArboretumAnnotation 实现:

#import "ArboretumAnnotation.h"


@implementation ArboretumAnnotation

@synthesize title;
@synthesize subtitle;
@synthesize image;
@synthesize coordinate;

-init
{
    return self;
}

-initWithCoordinate:(CLLocationCoordinate2D)inCoord
{
    coordinate = inCoord;
    return self;
}

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = /*some coord*/;
    theCoordinate.longitude = /*some coord*/;
    return theCoordinate; 
}

@end

我不知道我做错了什么,我添加的第一个注释显示,但第二个注释不显示。有什么想法吗?提前致谢!

So I have successfully added a single annotation to the map view with a custom image, however if I add a second annotation it merely refuses to display on the map.

Here is the relevant code:

- (void)viewDidLoad {
    [super viewDidLoad];
    //mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate = self;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Location Info" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(showCurrentLocationButtonTapped:)];

    [self loadOverlays];

    //TEST LOAD TWO ANNOTATIONS
    //MKPointAnnotation *testAnnot = [[MKPointAnnotation alloc] init];
    ArboretumAnnotation *arboAnnot = [[ArboretumAnnotation alloc] init];
    CLLocationCoordinate2D workingCoord;
    workingCoord.latitude = /*some coord*/;
    workingCoord.longitude = /*some coord*/;
    [arboAnnot setCoordinate:workingCoord];
    [arboAnnot setTitle:@"Test Title"];
    [arboAnnot setSubtitle:@"Test Subtitle"];
    [mapView addAnnotation:arboAnnot];
    [arboAnnot release];

    ArboretumAnnotation *arboAnnot2 = [[ArboretumAnnotation alloc] init];
    CLLocationCoordinate2D workingCoord2;
    workingCoord2.latitude = /*some coord*/;
    workingCoord2.longitude = /*some coord*/;
    [arboAnnot2 setCoordinate:workingCoord2];
    [arboAnnot2 setTitle:@"Test Title2"];
    [arboAnnot2 setSubtitle:@"Test Subtitle2"];
    [mapView addAnnotation:arboAnnot2];
    [arboAnnot2 release];
//...
}
//...
- (ArboretumAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:MKUserLocation.class]) {
        //it's the built-in user location annotation(blue dot)
        return nil;
    }

    NSString *annotIdentifier = @"annotation";

    MKPinAnnotationView *recycledAnnotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotIdentifier];
    if (!recycledAnnotationView) {
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:annotIdentifier] autorelease];

        UIImage *iconImage = [UIImage imageNamed:@"arboretum.png"];

        CGRect resizeRect;

        resizeRect.size = iconImage.size;
        CGSize maxSize = CGRectInset(self.view.bounds,
                                     10.0f,
                                     10.0f).size;
        maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0f;
        if (resizeRect.size.width > maxSize.width)
            resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
        if (resizeRect.size.height > maxSize.height)
            resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

        customPinView.image = iconImage;
        //customPinView.image.frame = CGRectMake(kBorder, kBorder, kWidth - 2 * kBorder, kWidth - 2 * kBorder);
        customPinView.opaque = NO;
        //customPinView.pinColor = MKPinAnnotationColorPurple;
        customPinView.canShowCallout = YES;
        return customPinView;
    } else {
        recycledAnnotationView.annotation = annotation;
    }
    return recycledAnnotationView;

}

ArboretumAnnotation header:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface ArboretumAnnotation : NSObject <MKAnnotation> {
    NSString *title;
    NSString *subtitle;
    UIImage *image;

    CLLocationCoordinate2D coordinate;

}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) UIImage *image;

@property (nonatomic) CLLocationCoordinate2D coordinate;

@end

ArboretumAnnotation implementation:

#import "ArboretumAnnotation.h"


@implementation ArboretumAnnotation

@synthesize title;
@synthesize subtitle;
@synthesize image;
@synthesize coordinate;

-init
{
    return self;
}

-initWithCoordinate:(CLLocationCoordinate2D)inCoord
{
    coordinate = inCoord;
    return self;
}

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = /*some coord*/;
    theCoordinate.longitude = /*some coord*/;
    return theCoordinate; 
}

@end

I have no idea what I am doing wrong, the first annotation I add displays, but the second one doesn't. Any ideas? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2024-10-26 09:46:28

我在发帖后几秒钟就想出了这个问题......

它是以下代码:

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = /*some coord*/;
    theCoordinate.longitude = /*some coord*/;
    return theCoordinate; 
}

我完全摆脱了这个功能,它解决了我的问题。抱歉打扰了!

I figured it out literally seconds after I made the post...

It was the following code:

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = /*some coord*/;
    theCoordinate.longitude = /*some coord*/;
    return theCoordinate; 
}

I got rid of this function altogether and it solved my problem. Sorry for the bother!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文