为什么 MKMapView 委托方法没有被调用?

发布于 2024-11-03 11:50:00 字数 3231 浏览 2 评论 0原文

我对 iPhone 开发非常陌生,我正在尝试注释地图。我已经能够通过硬编码坐标在地图上获取 3 个位置点,现在我正在尝试自定义图钉。我知道要做到这一点,您需要实现:-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id)annotation { 委托方法。

由于某种原因,我什至无法调用它。这是我在控制器头中的内容:

#import "ArtPiece.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface TestViewController : UIViewController <MKMapViewDelegate> {
    MKMapView *mapView;
    NSMutableArray *mapAnnotations;
    float results_lat[3];
    float results_long[3];
    NSMutableArray *results_title;
    NSMutableArray *Arts;

}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) NSMutableArray *mapAnnotations;

@end

这是我在控制器实现中的内容:

- (void)viewDidLoad {
    results_long[0] = -122.477989;
    results_lat[0] = 37.810000;
    results_long[1] = -122.480000;
    results_lat[1] = 37.820000;
    results_long[2] = -122.4850000;
    results_lat[2] = 37.830000;

    self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];
    Arts = [[NSMutableArray alloc] initWithCapacity:3];
    for(int x = 0; x<3; x++)
    {
        // creates an ArtPiece object and sets its lat and long
        ArtPiece *a = [[ArtPiece alloc] init];
        a.longitude = [NSNumber numberWithDouble:results_long[x]];
        a.latitude = [NSNumber numberWithDouble:results_lat[x]];
        a.title = @"please show up";
        // add objects to annotation array
        [self.mapAnnotations insertObject:a atIndex:x];
        [a release];

    }
    // center screen on cali area
    [self gotoLocation];
    for(int x = 0; x<3; x++)
    {
        [mapView addAnnotations:mapAnnotations];
    }
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    NSLog(@"This is not printing to to console...");
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorPurple; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

这是 ArtPiece 类的实现:

   #import "TestViewController.h"
    #import "ArtPiece.h"
    #import <MapKit/MapKit.h>
    #import <CoreData/CoreData.h>

    @implementation ArtPiece

    @synthesize title, artist, series, description, latitude, longitude;

    - (CLLocationCoordinate2D)coordinate
    {
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = [self.latitude doubleValue];
        theCoordinate.longitude = [self.longitude doubleValue];
        return theCoordinate;
    }



    @end

这可能很简单。我的方法声明有问题吗?我声明代表错误了吗?我再次无法弄清楚,我对这个 Objective C/Delegate 的东西非常陌生。感谢您的任何帮助。

I'm very new to iphone development and I'm trying to annotate a map. I've been able to get 3 location points on a map with hardcoded coordinates and now I'm trying to customize the pins. I know to do that you need to implement the:-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation { delegate method.

For some reason I've been unable to even get it called. Here is what I have in the controller header:

#import "ArtPiece.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface TestViewController : UIViewController <MKMapViewDelegate> {
    MKMapView *mapView;
    NSMutableArray *mapAnnotations;
    float results_lat[3];
    float results_long[3];
    NSMutableArray *results_title;
    NSMutableArray *Arts;

}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) NSMutableArray *mapAnnotations;

@end

And here is what I have in the controller implementation:

- (void)viewDidLoad {
    results_long[0] = -122.477989;
    results_lat[0] = 37.810000;
    results_long[1] = -122.480000;
    results_lat[1] = 37.820000;
    results_long[2] = -122.4850000;
    results_lat[2] = 37.830000;

    self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];
    Arts = [[NSMutableArray alloc] initWithCapacity:3];
    for(int x = 0; x<3; x++)
    {
        // creates an ArtPiece object and sets its lat and long
        ArtPiece *a = [[ArtPiece alloc] init];
        a.longitude = [NSNumber numberWithDouble:results_long[x]];
        a.latitude = [NSNumber numberWithDouble:results_lat[x]];
        a.title = @"please show up";
        // add objects to annotation array
        [self.mapAnnotations insertObject:a atIndex:x];
        [a release];

    }
    // center screen on cali area
    [self gotoLocation];
    for(int x = 0; x<3; x++)
    {
        [mapView addAnnotations:mapAnnotations];
    }
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    NSLog(@"This is not printing to to console...");
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorPurple; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

And here is the ArtPiece class implementation:

   #import "TestViewController.h"
    #import "ArtPiece.h"
    #import <MapKit/MapKit.h>
    #import <CoreData/CoreData.h>

    @implementation ArtPiece

    @synthesize title, artist, series, description, latitude, longitude;

    - (CLLocationCoordinate2D)coordinate
    {
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = [self.latitude doubleValue];
        theCoordinate.longitude = [self.longitude doubleValue];
        return theCoordinate;
    }



    @end

It's probably something simple. Is there something wrong with my method declaration? Did I declare the delegate wrong? I can't figure it out, again, I'm very new to this objective c/delegate stuff. Thanks for any help.

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

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

发布评论

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

评论(1

凝望流年 2024-11-10 11:50:00

addAnnotations 方法需要一个符合 MKAnnotation 协议的 NSArray 对象。

您正在添加 ArtPiece 类型的对象,该对象似乎没有实现具有 cooperative 属性的 MKAnnotation(不是 latitudelongitude< /代码> 属性)。

更新您的 ArtPiece 类以符合 MKAnnotation(或使用预定义的 MKPointAnnotation 类)。但更新您的自定义类是一个更好的解决方案。

The addAnnotations method expects an NSArray of objects that conform to the MKAnnotation protocol.

You are adding objects of type ArtPiece which don't seem to implement MKAnnotation which has a coordinate property (not latitude and longitude properties).

Update your ArtPiece class to conform to MKAnnotation (or use the pre-defined MKPointAnnotation class). But updating your custom class is a better fix.

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