我有一個名爲Device
的自定義類,它實現了MKAnnotation
協議。在我遵循的示例(O'Reilly Media的iPad上的MapKit和Core Location)中,他們說要檢查我要添加的註釋是否爲MKUserLocation
類,如果是,則返回nil。我完全理解那是什麼,但問題是我的Device
類始終標識爲MKUserLocation
,因此它始終返回nil,因此我從來沒有將任何註釋添加到地圖中。我一遍又一遍地重複了代碼。我也有O'Reilly代碼示例,我看不到要去哪裏。這真是令人沮喪。自定義MKAnnotation總是說它是一個MKUserLocation類
這裏是我的Device.m
:
@implementation Device
@synthesize udId, user, latitude, longitude;
- (CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D internalCoordinate;
internalCoordinate.latitude = [self.latitude doubleValue];
internalCoordinate.longitude = [self.longitude doubleValue];
return internalCoordinate;
}
- (NSString *)title {
return self.user;
}
- (NSString *)subtitle {
return nil;
}
- (id)initWithUDId:(NSString *)_udId User:(NSString *)_user Latitude:(NSNumber *)_latitude Longitude:(NSNumber *)_longitude {
if (self == [super init]) {
self.udId = _udId;
self.user = _user;
self.latitude = _latitude;
self.longitude = _longitude;
}
return self;
}
- (void)dealloc {
[udId release];
self.udId = nil;
[user release];
self.user = nil;
[latitude release];
self.latitude = nil;
[longitude release];
self.longitude = nil;
[super dealloc];
}
@end
這是我的DeviceMapAnnotator.m
:
@implementation DeviceMapAnnotator
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
NSLog(@"annotation is an MKUserLocation class");
return nil;
}
MKPinAnnotationView *deviceAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"DeviceAnnotation"];
if (deviceAnnotationView == nil) {
deviceAnnotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DeviceAnnotation"] autorelease];
deviceAnnotationView.animatesDrop = NO;
deviceAnnotationView.pinColor = MKPinAnnotationColorRed;
}
return deviceAnnotationView;
}
- (void)dealloc {
[super dealloc];
}
@end
而這裏的代碼我DashboardViewController.m
調用它:
- (void)updateMapAnnotations:(NSArray *)devices {
for (Device *device in devices) {
[map addAnnotation:device];
}
}
而這裏的代碼中調用updateMapAnnotations
從我的應用程序代表:
- (void)requestFinished:(ASIHTTPRequest *)request {
if (![request error]) {
NSError *jsonError = nil;
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithJSONString:[request responseString] error:&jsonError];
if (!jsonError || ([[jsonDictionary objectForKey:@"Success"] intValue] == 1)) {
NSArray *jsonDevicesArray = [jsonDictionary objectForKey:@"Devices"];
NSMutableArray *devicesArray = [[NSMutableArray alloc] initWithCapacity:[jsonDevicesArray count]];
for (NSDictionary *deviceDictionary in jsonDevicesArray) {
[devicesArray addObject:[[[Device alloc] initWithUDId:[deviceDictionary objectForKey:@"UDId"] User:[deviceDictionary objectForKey:@"User"] Latitude:[NSNumber numberWithDouble:[[deviceDictionary objectForKey:@"Latitude"] doubleValue]] Longitude:[NSNumber numberWithDouble:[[deviceDictionary objectForKey:@"Longitude"] doubleValue]]] autorelease]];
}
[dashboardViewController updateMapAnnotations:devicesArray];
} else {
// AUTHORIZATION FAILED
}
}
}
我基本上撥打電話到服務器的每45秒,獲得設備和它們的位置作爲一個JSON字符串,然後反序列化到含有Device
對象的NSArray
的列表。然後我將數組傳遞給updateMapAnnotations
,然後將其循環並調用addAnnotation
。整個過程是有效的,我保證發送到DeviceMapAnnotator
的對象的類型爲Device
,但MKUserLocation
檢查總是過早返回,整個過程只停在水中。
如果有人知道他們在用iOS/Objective-C等做什麼可以幫助我(這幾乎是每個人,因爲我顯然是一個白癡),我真的很感激。
只是爲了發泄,我必須說Objective-C真的很快得到我的$ hit列表。
不知道它的原因,但在initWithUDId,`如果(自== [超級初始化])`應該是`如果(自= [超級初始化])`。如果你把`NSLog(@「ann =%@」,annotation);`作爲viewForAnnotation的第一行,它是否會顯示你的設備註釋?另外,爲了可讀性,可維護性和調試的簡單性,您應該將真正長的devicesArray addObject行分解爲多個語句。 – Anna 2011-02-09 02:48:12
不幸的是,修復條件似乎沒有效果。 NSLog一直說它是`MKUserLocation` ... – Gup3rSuR4c 2011-02-09 06:05:58