2

キャッシュポイントの場所を表示するマップビューがあります。注釈は削除され、コールアウトをクリックして、その場所の詳細が記載されたページに移動できます。キャッシュポイントには無料と有料の2つのカテゴリがあり、無料のキャッシュポイントピンは緑と赤です。ピンが落ちるとき、それらは正しい色です。ユーザーの場所またはマップの他の領域にズームするまではすべて正常に機能します。その後、ピンに戻ると、色の書式が失われ、すべて元の赤色になります。

これは、タイルをダウンロードするときにマップがリロードされ、ピンが適切にリロードされないことと関係があると思いますが、間違っている可能性があります。

どんな助けでも大歓迎です。

これが私のコードです:

#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
4

1 に答える 1

3

マップのタイルの再読み込みに関連する問題ではありません。

問題は、デリゲートのコードが、デリゲートメソッドが呼び出されるたびにクラスインスタンスレベルのオブジェクトが同期されるという誤った仮定でオブジェクトをviewForAnnotation使用していることです。annann

デリゲートは、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です。これにより、注釈が多い場合のパフォーマンスが向上します。

于 2012-11-08T15:10:16.550 に答える