2013-07-30 47 views
0

我已經有這個問題幾個星期了,我仍然沒有找到答案。在我的MapView上我有自定義註釋,當我點擊「重新加載按鈕」時,所有信息都是正確的,如註解「標題,副標題」。但註釋已經改變。註釋是在NSMutableArray,我確信我正在圍繞這個問題展開討論。這裏是我用來重新加載註釋的代碼。自定義註解在MKMapView上重新加載時切換

所以只是爲了防止任何混淆,我的自定義註釋工作得很好,當我第一次加載mapView。但是一旦我點擊重新加載按鈕,所有註釋的信息(如「位置,標題,副標題」)都是正確的,只是實際的註釋已經改變。就像所有的註釋都被切換了一樣。 如果有人可以幫忙,將不勝感激!謝謝!

- (IBAction)refreshMap:(id)sender { 
NSArray *annotationsOnMap = myMapView.annotations; 
[myMapView removeAnnotations:annotationsOnMap]; 
[locations removeAllObjects]; 
[citiesArray removeAllObjects]; 
[self retrieveData]; 
} 


-(void) retrieveData { 
userLAT = [NSString stringWithFormat:@"%f", myMapView.userLocation.coordinate.latitude]; 
userLNG = [NSString stringWithFormat:@"%f", myMapView.userLocation.coordinate.longitude]; 

NSString *fullPath = [mainUrl stringByAppendingFormat:@"map_json.php?userID=%@&lat=%@&lng=%@",theUserID,userLAT,userLNG]; 

NSURL * url =[NSURL URLWithString:fullPath]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 

json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 


citiesArray =[[NSMutableArray alloc]init]; 
for (int i = 0; i < json.count; i++) 
{ 
    //create city object 
    NSString * eID =[[json objectAtIndex:i] objectForKey:@"userid"]; 
    NSString * eAddress =[[json objectAtIndex:i] objectForKey:@"full_address"]; 
    NSString * eHost =[[json objectAtIndex:i] objectForKey:@"username"]; 
    NSString * eLat =[[json objectAtIndex:i] objectForKey:@"lat"]; 
    NSString * eLong =[[json objectAtIndex:i] objectForKey:@"lng"]; 
    NSString * eName =[[json objectAtIndex:i] objectForKey:@"Restaurant_name"]; 
    NSString * eState = [[json objectAtIndex:i] objectForKey:@"type"]; 
    NSString * annotationPic = [[json objectAtIndex:i] objectForKey:@"Annotation"]; 
    NSString * eventID = [[json objectAtIndex:i] objectForKey:@"id"]; 

    //convert lat and long from strings 

    float floatLat = [eLat floatValue]; 
    float floatLONG = [eLong floatValue]; 
    City * myCity =[[City alloc] initWithRestaurantID: (NSString *) eID andRestaurantName: (NSString *) eName andRestaurantState: (NSString *) eState andRestaurantAddress: (NSString *) eAddress andRestaurantHost: eHost andRestaurantLat: (NSString *) eLat andRestaurantLong: (NSString *) eLong]; 
    //Add our city object to our cities array 
    // Do any additional setup after loading the view. 

    [citiesArray addObject:myCity]; 

    //Annotation 

    locations =[[NSMutableArray alloc]init]; 
    CLLocationCoordinate2D location; 
    Annotation * myAnn; 

    //event1 annotation 
    myAnn =[[Annotation alloc]init]; 
    location.latitude = floatLat; 
    location.longitude = floatLONG; 
    myAnn.coordinate = location; 
    myAnn.title = eName; 
    myAnn.subtitle = eHost; 
    myAnn.type = eState; 
    myAnn.AnnotationPicture = annotationPic; 
    myAnn.passEventID = eventID; 
    myAnn.hotZoneLevel = hotZone; 
    [locations addObject:myAnn]; 
    [self.myMapView addAnnotations:locations]; 

} 

} 



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

if([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

static NSString *annotationIdentifier = @"AnnotationIdentifier"; 

MKAnnotationView *annotationView = (MKAnnotationView *) [self.myMapView 
                 dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 

if (!annotationView) 
{ 
    annotationView = [[MKAnnotationView alloc] 
         initWithAnnotation:annotation 
         reuseIdentifier:annotationIdentifier]; 

    NSString *restaurant_Icon = ((Annotation *)annotation).AnnotationPicture; 
    NSString *restaurant_Callout = [NSString stringWithFormat:@"mini.%@",restaurant_Icon]; 

    UIImage *oldImage = [UIImage imageNamed:restaurant_Icon]; 
    UIImage *newImage; 
    CGSize newSize = CGSizeMake(75, 75); 

    newImage = [oldImage imageScaledToFitSize:newSize]; // uses MGImageResizeScale 


    annotationView.image= newImage; 

    annotationView.canShowCallout = YES; 

    UIImage *Mini_oldImage = [UIImage imageNamed:event_Callout]; 
    UIImage *Mini_newImage; 
    CGSize Mini_newSize = CGSizeMake(30,30); 

    Mini_newImage = [Mini_oldImage imageScaledToFitSize:Mini_newSize]; // uses MGImageResizeScale 

    UIImageView *finalMini_callOut = [[UIImageView alloc] initWithImage:Mini_newImage]; 
    annotationView.leftCalloutAccessoryView = finalMini_callOut; 


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 


    annotationView.rightCalloutAccessoryView = rightButton; 

} 
else 
{ 
    annotationView.annotation = annotation; 
} 

return annotationView; 
} 

回答

1

如果不出意外,你設置的圖標和基於註釋的標註,但只有這樣做,在viewForAnnotation如果註解沒有離隊。如果重新使用註解視圖,您真的想在塊之外執行任何特定於註釋的自定義。


無關您報告的問題,還有一些其他的意見:

  1. 你或許應該做retrieveData異步這樣你就不會佔用主線程與您的數據檢索/分析。繼續發送添加條目到你的數組,並添加註釋到主隊列中的地圖,但網絡的東西,絕對應該做異步。

  2. 你或許應該檢查,以確保datanil(例如,沒有網絡連接或其他網絡故障),因爲如果你傳遞一個nilJSONObjectWithData會崩潰。

  3. 您使用的locations似乎沒有必要,因爲你重置它在你的JSON每個條目。您可以(a)完全退出locations,只需將myAnn對象添加到您的地圖註釋;或(b)在for循環之前初始化locations。但是維護這個ivar可能會產生誤導,但只能用最後一個註釋填充它。