2011-02-09 26 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]; 

}

相關問題