2012-03-08 55 views
1

在我的viewcontroller我使用地圖,我加載一個引腳列表。 當我移動地圖或放大或縮小它,我的應用崩潰並顯示此錯誤:GMMGeoTileImageData錯誤與MapKit

[GMMGeoTileImageData isEqualToString:]: unrecognized selector sent to instance 0x862d3b0 

這是我的視圖控制器的代碼:

- (void)viewDidLoad 
    { 

     statoAnn = [[NSMutableString alloc] initWithFormat:@"false"]; 

     //bottone annulla per tornare indietro 
     UIBarButtonItem *annullaButton = [[[UIBarButtonItem alloc] initWithTitle:@"Annulla" style:UIBarButtonItemStylePlain target:self action:@selector(backView)] autorelease]; 
     self.navigationItem.leftBarButtonItem = annullaButton; 


     //inizializzo la mappa 
     mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]; 
     mapView.delegate = self; 
     mapView.mapType = MKMapTypeStandard; 
     [self.view addSubview:mapView]; 

     [self setGmaps:arrData]; 

     [super viewDidLoad]; 
    } 


/** inizializzo l'annotation del poi mappa **/ 
- (void) setGmaps:(NSMutableArray*)inputData { 

    // setto la lat e lng 
    CLLocationDegrees latitude; 
    CLLocationDegrees longitude; 
    CLLocationCoordinate2D poiLocation; 
    arrAnn = [[NSMutableArray alloc] init]; 

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

     //ricavo la lat e lng del pin 
     latitude = [[[inputData objectAtIndex:i] objectForKey:@"latitude"] doubleValue]; 
     longitude = [[[inputData objectAtIndex:i] objectForKey:@"longitude"] doubleValue]; 

     // setto la location del poi 
     poiLocation.latitude = latitude; 
     poiLocation.longitude = longitude; 
     //[[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease]; 

     //setto il pin 
     Annotation *ann = [[Annotation alloc] initWithCoordinate:poiLocation]; 
     ann.title = [[inputData objectAtIndex:i] objectForKey:@"label"]; 
     [arrAnn addObject:ann]; 
     [ann release]; 

    } 


    if (nil != self.arrAnn) { 

     [self.mapView addAnnotations:arrAnn]; 
     //self.ann = nil; 
     self.arrAnn = nil; 
    } 

} 

/** setto il pin nella mappa ***/ 
- (void)setCurrentLocation:(CLLocation *)location { 

    MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}}; 

    region.center = location.coordinate; 

    region.span.longitudeDelta = 0.1f; 
    region.span.latitudeDelta = 0.1f; 

    [self.mapView setRegion:region animated:YES]; 
    [self.mapView regionThatFits:region]; 
} 


- (MKAnnotationView *)mapView:(MKMapView *)mapViewTemp viewForAnnotation:(id <MKAnnotation>)annotation { 

    MKPinAnnotationView *view = nil; // return nil for the current user location 

     view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"identifier"]; 

     if (nil == view) { 
      view = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"identifier"] autorelease]; 
      view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     } 

     [view setPinColor:MKPinAnnotationColorPurple]; 
     [view setCanShowCallout:YES]; 
     [view setAnimatesDrop:YES]; 

    if (![statoAnn isEqualToString:@"true"]) { 
     CLLocation *location = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude 
                  longitude:annotation.coordinate.longitude]; 
     [self setCurrentLocation:location]; 
     statoAnn = [NSMutableString stringWithFormat:@"true"]; 
    } 


    return view; 
} 

回答

2

viewForAnnotation,這條線:

statoAnn = [NSMutableString stringWithFormat:@"true"]; 

statoAnn設置爲自動釋放字符串。

當方法退出時,releasestatoAnn上被調用,並且它不再擁有它指向的內存。當您縮放或移動地圖時再次調用該方法時,statoAnn指向的內存現在由其他內容(本例中爲GMMGeoTileImageData)使用。該對象不是NSString,並且沒有isEqualToString:方法,您會看到您看到的錯誤。

若要解決此問題,請設置statoAnn,以便像在viewDidLoad中那樣保留該值。例如,你可以將其更改爲:

statoAnn = [[NSMutableString alloc] initWithFormat:@"true"]; 

你也可以聲明statoAnn作爲一個屬性(@property (nonatomic, copy) NSString *statoAnn),只是它使用self.statoAnn = @"true";設置。財產二傳手將爲您做保留。


但是,您不需要使用字符串來保存「true」和「false」值。使用簡單的方式更容易和高效,因爲它是原始類型而不是對象,所以您不必擔心保留/釋放。


的另一件事是,viewForAnnotation是不正確的地方要設置地圖視圖的region擺在首位。添加註釋後,您可以在viewDidLoad中執行此操作。


另一件事:在viewForAnnotation的頂部,你有註釋「return nil for the current user location」,但該代碼不這樣做。它只是將視圖初始化爲nil。要真正做評論說什麼,你需要這樣的:

MKPinAnnotationView *view = nil; 

// return nil for the current user location... 
if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 


最後,如果dequeueReusableAnnotationViewWithIdentifier不會返回一個視圖(如視圖=零!),你需要設置view.annotation當前的註解,因爲重-used視圖可能用於不同的註釋。

+0

太棒了!這是我的解決方案;) – Dany 2012-03-09 12:26:27