2013-03-27 20 views
0

當刷新完成時,PIn color mapView有問題。 在我的應用程序中,我用兩種顏色顯示某個點,以確定服務是否可用。 第一次啓動時,不會出現問題。該代碼是跟隨者:MapView問題 - 在地圖重新編碼時Pin改變顏色

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self dowloadPoint]; // here I exucte the first start 

} 

- (void)dowloadPoint{ 
    NSURL *url1 =[NSURL URLWithString:@"http:MYUSRL"]; 
    NSData *datos1 =[[NSData alloc] initWithContentsOfURL:url1]; 

     [self plotBarPosition:datos_string1];  //Here I call the plotBarPosition method 
} 


- (void)plotBarPosition:(NSString *)datos_string1 { 

    for (id<MKAnnotation> annotation in _mapView.annotations) { 
     [_mapView removeAnnotation:annotation]; 
    } 

    // Parse the string into JSON 
    NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"]; 

    // Get the objects you want, e.g. output the second item's client id 
    NSArray *items_properties = [json valueForKeyPath:@"properties"]; 
    NSArray *items_geo = [json valueForKeyPath:@"geometry"]; 

     for (int i = 0; i < [json count]; i++){ 

      NSString *nomprePunto =[[items_properties objectAtIndex:i] objectForKey:@"title"]; 

      NSNumber *lat =[[[items_geo objectAtIndex:i] objectForKey:@"coordinates"] objectAtIndex:0]; 


      NSNumber *lon =[[[items_geo objectAtIndex:i] objectForKey:@"coordinates"] objectAtIndex:1]; 

      CLLocationCoordinate2D coordinate; 
      coordinate.latitude = lat.doubleValue; 
      coordinate.longitude = lon.doubleValue; 

      //ESTADO 
      NSString *description = [[items_properties objectAtIndex:i] objectForKey:@"description"]; 
      NSString *estado_punto = [[NSString alloc]init]; 

      if ([description rangeOfString:@"Averiado"].location == NSNotFound) { 
       estado_punto = @"Available"; 
      } else { 
       estado_punto = @"NOt Available"; 
       averiados ++; 
      } 
      NSString *averiadosStr = [NSString stringWithFormat:@"%d",averiados]; 
      averiadosLabel.text = averiadosStr; 

      MyLocation *location =[[MyLocation alloc] initWithName:nomprePunto coordinate:coordinate estado:estado_punto]; 

      [_mapView addAnnotation:location]; 
    } 


} 



- (MKPinAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyLocation *)annotation { 

     static NSString *identifier = @"MyLocation"; 
     if ([annotation isKindOfClass:[MyLocation class]]) { 
      MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

      if (annotationView == nil) { 
       annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 

       annotationView.enabled = YES; 
       annotationView.canShowCallout = YES; 

       if([[annotation estado] isEqualToString:@"En Servicio"]) 
       annotationView.pinColor = MKPinAnnotationColorGreen; 

       } else { 
       annotationView.annotation = annotation; 
      } 

      return annotationView; 
     } 

     return nil; 
    } 

但磨片我添加refres按鈕是功能僅僅是一個refreshcalling的dowloadPoint再次

- (IBAction)refresh{ 

    [self dowloadPoint]; 
} 

銷的顏色「隨機方式」改變,而不是與真實的觀點相符合。 有關正在發生的事情的任何想法?提前致謝。

編輯:seemps pproblems是由於:

for (id<MKAnnotation> annotation in _mapView.annotations) { 
    [_mapView removeAnnotation:annotation]; 
} 

刪除它,應用程序工作正常,但銷區淹沒abow前面的...:S

回答

1

引腳的默認顏色是紅色的。如果MyLocation對象的estado屬性等於@"En Servicio",則將其設置爲綠色。我知道有時顏色是紅色的,當您的estado屬性等於@"En Servicio"時,或者有時候是綠色,如果不是。
原因之一可能是當您按下刷新按鈕時,您的MyLocation對象不再存在。在這種情況下,您可能仍然有一個指向曾經存在的內存位置的指針,但該位置可能已被任何內容覆蓋,導致隨機顏色。
這可能發生在例如如果您的MyLocation對象已創建爲在您返回到主事件循環時已釋放的自動釋放對象,即處理用戶交互。
如果您使用ARC,則不應該如此。

+0

嗨。謝謝你的回答。我使用ARC所以問題不依賴於發佈,是嗎? – doxsi 2013-03-27 15:36:35

+0

嗨,我只有一個更多的想法:MKAnnotationView類參考的文檔說它的註釋屬性:「你不應該直接改變這個屬性的值。」但是,您在聲明annotationView.annotation = annotation;中執行此操作。也許這會導致問題? – 2013-03-27 19:39:23

+0

這個問題似乎是由於使用了:for(idmap_annotations中的註解)。 }如果我刪除它可以正常工作,但引腳被淹沒在以前的引腳之上。 – doxsi 2013-03-27 20:58:58