为什么 MKMapView 委托方法没有被调用?
我对 iPhone 开发非常陌生,我正在尝试注释地图。我已经能够通过硬编码坐标在地图上获取 3 个位置点,现在我正在尝试自定义图钉。我知道要做到这一点,您需要实现:-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id
委托方法。
由于某种原因,我什至无法调用它。这是我在控制器头中的内容:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
addAnnotations
方法需要一个符合MKAnnotation
协议的 NSArray 对象。您正在添加
ArtPiece
类型的对象,该对象似乎没有实现具有cooperative
属性的 MKAnnotation(不是latitude
和longitude< /代码> 属性)。
更新您的
ArtPiece
类以符合MKAnnotation
(或使用预定义的MKPointAnnotation
类)。但更新您的自定义类是一个更好的解决方案。The
addAnnotations
method expects an NSArray of objects that conform to theMKAnnotation
protocol.You are adding objects of type
ArtPiece
which don't seem to implement MKAnnotation which has acoordinate
property (notlatitude
andlongitude
properties).Update your
ArtPiece
class to conform toMKAnnotation
(or use the pre-definedMKPointAnnotation
class). But updating your custom class is a better fix.