为什么不调用MKMapView委托方法?
问题描述:
我对iphone开发非常陌生,我试图给注解一张地图。我已经能够在具有硬编码坐标的地图上获得3个位置点,现在我正在尝试定制这些引脚。我知道你需要实现:-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
委托方法。为什么不调用MKMapView委托方法?
出于某种原因,我一直无法得到它叫。以下是我在控制器标题:
#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
这可能很简单。我的方法声明有什么问题吗?我是否宣布委托人有错?我无法弄清楚,再次,我对这个客观的C /委托的东西很陌生。谢谢你的帮助。
答
addAnnotations
方法需要一个符合MKAnnotation
协议的NSArray对象。
您要添加ArtPiece
类型的对象,这似乎并没有实现MKAnnotation具有coordinate
财产(不latitude
和longitude
属性)。
更新您的ArtPiece
类以符合MKAnnotation
(或使用预定义的MKPointAnnotation
类)。但更新您的自定义类是一个更好的解决方案。
非常感谢您的反馈!我编辑了我的问题以包含ArtPiece类。我认为它有由经纬度数据成员设置的坐标协议。我认为它必须符合MKAnnotation协议,因为我的地图确实显示了默认的红色针脚。但也许我错了?看看ArtPiece课程是否给你任何其他想法或确认你的第一个答案? – Ryan 2011-04-26 22:08:20
好吧,如果你确实看到至少红色的针脚,那么ArtPiece必须没问题。然后它就像下面这样简单:在Interface Builder中,您是否将地图视图的委托连接到TestViewController的文件所有者? – Anna 2011-04-26 22:11:53
如果代理连接在IB中正常,则发布ArtPiece.h文件。 – Anna 2011-04-26 22:18:10