2012-11-08 39 views
2

我有一個顯示收銀點位置的mapview。註釋將被刪除,並且可以點擊標註以轉到具有關於該位置的更多詳細信息的頁面。有兩種類型的自動取款機,免費和付費,免費的自動提款機插針爲綠色,另一種爲紅色。當針腳掉落時,它們是正確的顏色。一切正常,直到我縮放到用戶的位置或地圖的其他地區,然後當我回到他們已經失去了他們的顏色格式的引腳,並且都是原來的紅色。MapView Pin註解的問題 - 當地圖放大/平移/地區變化時Pin會丟失顏色

我認爲這是地圖重新加載時,它下載瓷磚並不正確地重新加載引腳,雖然我可能是錯的。

任何幫助將不勝感激。

這裏是我的代碼:

#import "CashPointMapViewController.h" 
#import "PinDrop.h" 
#import "CashPointDetailViewController.h" 

@implementation CashPointMapViewController 
@synthesize app, theCashList, mapview, ann, count, myArray, pinColor; 

- (IBAction) getlocation { 

    MKCoordinateRegion region; 
    region.center = self.mapview.userLocation.coordinate; 
    region.span.longitudeDelta = 0.01f; 
    region.span.longitudeDelta = 0.01f; 
    [mapview setRegion:region animated:YES]; 
} 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    mapview.showsUserLocation = YES; 

    [mapview setMapType:MKMapTypeStandard]; 
    [mapview setZoomEnabled:YES]; 
    [mapview setScrollEnabled:YES]; 

    MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region.center.latitude = 53.801279; 
    region.center.longitude = -1.548567; 
    region.span.longitudeDelta = 0.3f; 
    region.span.longitudeDelta = 0.3f; 
    [mapview setRegion:region animated:YES]; 

    app = [[UIApplication sharedApplication]delegate]; 

    UIImage *locate = [UIImage imageNamed:@"location arrow white.png"]; 

    UIBarButtonItem *userlocatebutton = [[UIBarButtonItem alloc] initWithImage:locate  style:UIBarButtonItemStylePlain target:self action:@selector(getlocation)]; 

    self.navigationItem.rightBarButtonItem = userlocatebutton; 

    [self performSelectorInBackground:@selector(annloop) withObject:self]; 

} 


-(void) annloop { 

    int i; 
    for (i=0; i<=count-1; i = i+1) { 
    theCashList = [myArray objectAtIndex:i]; 

    NSString *trimlat = [theCashList.lat stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSString *trimlon = [theCashList.lon stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    double latdouble = [trimlat doubleValue]; 
    double londouble = [trimlon doubleValue]; 

    CLLocationCoordinate2D coord = {(latdouble),(londouble)}; 

    ann = [[PinDrop alloc] init]; 

    ann.index = i; 

    ann.title = [theCashList.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    NSString *street = [theCashList.street stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSString *town = [theCashList.town stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSString *postcode = [theCashList.postcode stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    NSString *address = [[NSString alloc] initWithFormat:@"%@, %@, %@", street, town, postcode]; 

    ann.subtitle = address; 
    ann.coordinate = coord; 

    NSString *trimprice = [theCashList.price stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    if ([trimprice isEqualToString:@"Free"]) 
    { 
     ann.price = 1; 
    } 
    else 
    { 
     ann.price = 0; 
    } 
    [mapview performSelectorOnMainThread:@selector(addAnnotation:) withObject:ann waitUntilDone:YES]; 
    } 
} 


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

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

    MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:ann reuseIdentifier:@"current"]; 
    mypin.backgroundColor = [UIColor clearColor]; 
    UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    mypin.rightCalloutAccessoryView = goToDetail; 
    mypin.draggable = NO; 
    mypin.animatesDrop = TRUE; 
    mypin.canShowCallout = YES; 


    if (ann.price == 1) 
    { 
     mypin.pinColor = MKPinAnnotationColorGreen; 
    } 
    else 
    { 
     mypin.pinColor = MKPinAnnotationColorRed; 
    } 
    return mypin; 
    } 


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control { 

    PinDrop *annView = view.annotation; 
    CashPointDetailViewController *detailView = [[CashPointDetailViewController alloc]init]; 
    theCashList = [myArray objectAtIndex:annView.index]; 
    detailView.theCashList = theCashList; 
    [self.navigationController pushViewController:detailView animated:YES]; 

} 


- (void)viewDidUnload { 

    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

編輯:這是我的.h是否有幫助。

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 
#import "CashPointList.h" 
#import <MapKit/MapKit.h> 
#import "PinDrop.h" 

@interface CashPointMapViewController : UIViewController { 

    MKMapView *mapview; 
    PinDrop *ann; 
} 

    @property (nonatomic, retain) AppDelegate *app; 
    @property (nonatomic, retain) CashPointList *theCashList; 
    @property (nonatomic, retain) PinDrop *ann; 
    @property (nonatomic, retain) IBOutlet MKMapView *mapview; 
    @property (nonatomic, readwrite) int count; 
    @property (nonatomic, retain) NSMutableArray *myArray; 
    @property (nonatomic) MKPinAnnotationColor pinColor; 

    -(IBAction) getlocation; 


@end 

回答

3

這不是與地圖重新加載瓦片有關的問題。

的問題是,在viewForAnnotation委託的代碼使用的是ann對象與不正確的假設類實例級ann對象將同步與每當委託方法被調用。

viewForAnnotation委託不一定叫您添加註釋和可以被稱爲多個次同樣的註解如果地圖需要當談到回到視圖中重新顯示的標註順序。

當委託方法被再次調用以前添加的註釋時,annannotation不再指向同一個對象。 ann現在可能指向最後添加的註釋,因此所有註釋都會更改爲其顏色。

在該委託方法,您必須使用annotation參數,它是將地圖視圖要在當前呼叫爲視圖中的註釋引用(可能是完全無關,你的外循環)。

所以這行:

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc] 
    initWithAnnotation:ann reuseIdentifier:@"current"]; 

應該是:

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:@"current"]; 
         ^^^^^^^^^^ 

和檢查註釋的屬性時,使用annotation參數(和它投射到您的自定義類來獲得在自定義屬性):

PinDrop *ann = (PinDrop *)annotation; 
//Note that this local "ann" is NOT the same as the class-level "ann". 
//May want to use a different name to avoid confusion. 
//The compiler may also warn you about this. 

if (ann.price == 1) 
... 


單獨的,不相關但強烈推薦的建議是使用dequeueReusableAnnotationViewWithIdentifier來實現註釋視圖重用。當您有很多註釋時,這會提高性能。

+0

非常感謝你的工作。 Alex –

+0

適用於swift 2.0的任何解決方案? –