2011-02-09 54 views
1

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

+0

不知道它的原因,但在initWithUDId,`如果(自== [超级初始化])`应该是`如果(自= [超级初始化])`。如果你把`NSLog(@“ann =%@”,annotation);`作为viewForAnnotation的第一行,它是否会显示你的设备注释?另外,为了可读性,可维护性和调试的简单性,您应该将真正长的devicesArray addObject行分解为多个语句。 – Anna 2011-02-09 02:48:12

+0

不幸的是,修复条件似乎没有效果。 NSLog一直说它是`MKUserLocation` ... – Gup3rSuR4c 2011-02-09 06:05:58

回答

0

好的,我得到它的工作,但我不知道究竟是什么解决方案。最后,我放弃了一切,并在一个新文件中从头开始重写,并在那时起作用。我也意识到,我没有发回JSON中的纬度和纬度,但是这仍然不是问题,因为它们将被初始化为0.0。最后我不知道确实是固定它,但它的工作原理,所以我会拿它。

对不起,浪费你们的时间。 :(

0

我没有立即看到您的代码有任何问题。然而,而不是最初的测试,看是否标注是MKUserLocation,你可以改为不测试直接和测试注释是否是Device,像这样:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    if ([annotation isKindOfClass:[Device class]]) {  
     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; 
    } else { 
     NSLog(@"annotation is not a Device"); 

     return nil; 
    } 
} 

这可能不是一个非常有用的建议因为它基本上是一样的东西,但也许做一个稍微不同的方式会产生不同的结果,当调试和导致真正的问题。

0

我有一个类似的问题。显然,从mapView中删除注释会导致它们如何发布的一些问题。注释掉代码修复了它。

- (void)dealloc { 

//[mapView removeAnnotations:mapView.annotations]; 

[super dealloc]; 

}

相关问题