与MapView的引脚注释问题 - 失去脚的颜色,当地图缩放/平移/地区的变化(Problems w

2019-07-03 16:24发布

我有一个显示的现金点位置的MapView的。 注释被丢弃,可点击的标注去一个页面,该地点的更多细节。 有两类自动取款机,免费和付费的,免费的自动取款机针是绿色和其他红色。 当针掉落他们是正确的色彩。 一切正常,直到我放大到地图的用户位置或其他地区然后当我回去,他们已经失去了颜色格式的引脚和都是原来的红色。

我认为这是值得做的地图重新加载时,它下载砖和不正常重装针,虽然我可能是错的。

任何帮助都感激不尽。

这里是我的代码:

#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

Answer 1:

这不是与地图重装瓷砖问题。

问题是,在代码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 。 当你有很多注释这将提高性能。



文章来源: Problems with MapView Pin Annotation - Pin loses color when map is zoomed/panned/region changes