Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios MapView Pin注释存在问题-缩放/平移地图/更改区域时,Pin会失去颜色_Ios_Mkmapview_Mkannotation_Mkannotationview - Fatal编程技术网

Ios MapView Pin注释存在问题-缩放/平移地图/更改区域时,Pin会失去颜色

Ios MapView Pin注释存在问题-缩放/平移地图/更改区域时,Pin会失去颜色,ios,mkmapview,mkannotation,mkannotationview,Ios,Mkmapview,Mkannotation,Mkannotationview,我有一个地图视图,显示现金点的位置。注释将被删除,并且可以单击标注以转到包含该位置更多详细信息的页面。有两种类型的现金点,免费和付费,免费现金点PIN是绿色的,另一种是红色的。别针掉落时,颜色正确。一切正常,直到我缩放到用户位置或地图的其他区域,然后当我返回到PIN时,它们的颜色格式已丢失,并且都是原始的红色 我假设这与地图下载分幅时重新加载有关,但没有正确地重新加载插脚,尽管我可能错了 任何帮助都将不胜感激 这是我的密码: #import "CashPointMapViewController

我有一个地图视图,显示现金点的位置。注释将被删除,并且可以单击标注以转到包含该位置更多详细信息的页面。有两种类型的现金点,免费和付费,免费现金点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
#导入“CashPointMapViewController.h”
#导入“PinDrop.h”
#导入“CashPointDetailViewController.h”
@CashPointMapViewController的实现
@综合应用程序、现金清单、地图视图、人工神经网络、计数、myArray、pinColor;
-(iAction)getlocation{
协调区域;
region.center=self.mapview.userLocation.coordinate;
region.span.longitudeDelta=0.01f;
region.span.longitudeDelta=0.01f;
[地图视图设置区域:区域动画:是];
}
-(无效)viewDidLoad{
[超级视图下载];
mapview.showsUserLocation=是;
[mapview setMapType:MKMapTypeStandard];
[mapview SetZoomeEnabled:是];
[地图视图设置可克隆:是];
MKCoordinateRegion={0.0,0.0},{0.0,0.0};
region.center.lation=53.801279;
region.center.longitude=-1.548567;
region.span.longitudeDelta=0.3f;
region.span.longitudeDelta=0.3f;
[地图视图设置区域:区域动画:是];
app=[[UIApplication sharedApplication]委托];
UIImage*locate=[UIImage ImageName:@“location arrow white.png”];
UIBarButtonItem*userlocatebutton=[[UIBarButtonItem alloc]initWithImage:locate样式:UIBarButtonItemStylePlain目标:自我操作:@selector(getlocation)];
self.navigationItem.rightBarButtonItem=userlocatebutton;
[self-performSelectorInBackground:@selector(annloop)with object:self];
}
-(void)循环{
int i;

对于(i=0;i而言,这与贴图重新加载分幅无关

问题是,
viewForAnnotation
委托中的代码使用的是
ann
对象,错误地假设只要调用委托方法,类实例级
ann
对象就会与之同步

viewForAnnotation
委托不一定按照添加注释的顺序调用,如果地图返回视图时需要重新显示注释,则可以为同一注释多次调用委托

当为先前添加的批注再次调用委托方法时,
ann
annotation
不再指向同一个对象。
ann
现在可能指向最后添加的批注,因此所有批注都变为其颜色

在该委托方法中,必须使用
annotation
参数,该参数是地图视图希望在当前调用中查看的注释的引用(可能与外部循环完全无关)

所以这一行:

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

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:@"current"];
                       ^^^^^^^^^^
检查注释的属性时,请使用
注释
参数(并将其强制转换到自定义类以获取自定义属性):



一个单独的、不相关但强烈建议的建议是,通过使用
dequeueReusableAnnotationViewWithIdentifier
,实现注释视图重用。这将在有大量注释时提高性能。

swift 2.0有任何解决方案吗?
MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:@"current"];
                       ^^^^^^^^^^
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)
...