我對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 /委託的東西很陌生。謝謝你的幫助。
非常感謝您的反饋!我編輯了我的問題以包含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