我有一个名为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