【问题标题】:MKMapView custom pins according to the boolean valueMKMapView 根据布尔值自定义引脚
【发布时间】:2015-01-05 05:33:24
【问题描述】:

我有大约 190 个自定义图钉的 MKMap。我想根据布尔值更改引脚图像。但是我已经制作了静态布尔值,并且在 -(void)Map 中,我从 Parse 获取数据以及所有布尔值。当我在自定义引脚的委托方法中使用 If/Else 时,所有布尔值都变为 FALSE。

-(void)Map{
    CLLocation *userLoc = self.mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);

    self.mapView.delegate=self;
    NSMutableArray* annotations=[[NSMutableArray alloc] init];

    NSMutableArray *title = [self.itemss valueForKey:@"title"];
    NSMutableArray *subtitle = [self.itemss valueForKey:@"subtitle"];
    NSMutableArray *latitude = [self.itemss valueForKey:@"Lati"];
    NSMutableArray *longitude = [self.itemss valueForKey:@"Long"];
    NSMutableArray *gold = [self.itemss valueForKey:@"clubPurchased"];


    NSLog(@"%@", latitude);
    NSLog(@"%@", longitude);

    for (int i = 0; i < [title count]; i++){
        NSLog(@"%lu", (unsigned long)title.count);

        double lat = [[latitude objectAtIndex:i] doubleValue];
        double lng = [[longitude objectAtIndex:i] doubleValue];
        NSString *T = [title objectAtIndex:i];
        NSString *TT = [subtitle objectAtIndex:i];

         mine = [[gold objectAtIndex:i] boolValue];
        NSLog(@"BOOL:%hhd", mine);



        CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
        [annotations addObject:towerLocation];
        CLLocationCoordinate2D coord = [[annotations lastObject] coordinate];

        PlacePin * myAnnotation1=[[PlacePin alloc] init];

        myAnnotation1.coordinate=coord;
        myAnnotation1.title=T;
        myAnnotation1.subtitle=TT;

        [self.mapView addAnnotation:myAnnotation1];
        [SVProgressHUD dismiss];

        }



}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"welcome into the map view annotation");

    // if it's the user location, just return nil.


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

    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView* pinView = [[MKAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];



    if (mine){
        pinView.image = [UIImage imageNamed:@"Iconn"];
       NSLog(@"TRUE");


    } else {
       NSLog(@"FALSE");

        pinView.image = [UIImage imageNamed:@"Iconn_gold"];


    }
    pinView.canShowCallout=YES;



    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;


    return pinView;
}

【问题讨论】:

    标签: ios objective-c annotations mkmapview


    【解决方案1】:

    在 -(void)Map 中,mine 的值在 for 循环中发生了更改,它注册了循环中最后一项的值(这很可能是错误的)。

    viewForAnnotation 中,mine 的每个注释的值都没有改变,因此在显示注释时您会得到全部错误。

    您需要将 mine 值保存到自定义注释 PlacePin 中的 -(void)Map 中的每个项目(就像 titlesubtitle)并检索 viewForAnnotation 中的值作为 if -else 语句。

    另外,您可能无法正确访问viewForAnnotation 中的自定义注释,您可以参考此post

    编辑 2:

    我相信PlacePin 是您的自定义注释。在 PlacePin.h 文件中,

    @interface PlacePin : NSObject <MKAnnotation>
    
    @property(nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property(nonatomic, copy) NSString *title;
    @property(nonatomic, copy) NSString *subtitle;
    // Add this line
    @property (strong, nonatomic) NSNumber *mine;
    

    在 - (void)Map,

    PlacePin * myAnnotation1=[[PlacePin alloc] init];
    
    myAnnotation1.coordinate=coord;
    myAnnotation1.title=T;
    myAnnotation1.subtitle=TT;
    // Save the boolean value here
    myAnnotation1.mine=[gold objectAtIndex:i];
    

    在 viewForAnnotation 中,试试这个:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
    
        if ([annotation isKindOfClass:[PlacePin class]])
        {
            PlacePin *placePin = (PlacePin *)annotation;
            MKAnnotationView* pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
    
            if(pinView)
            {
                pinView.annotation = annotation;
            }
            else
            {
                // Edit 2: Note this line!!
                pinView = placePin.annotationView;
            }
            // Use this to check instead
            if ([pinView.mine boolValue])
            {
                pinView.image = [UIImage imageNamed:@"Iconn"];
                NSLog(@"TRUE");
            } 
            else 
            {
                NSLog(@"FALSE");
                pinView.image = [UIImage imageNamed:@"Iconn_gold"];
            }
            ...
        }
    }    
    

    【讨论】:

    • 我按照您建议的帖子重新制作了它,但是通过保存我的价值您的意思是用 NSUserDefaults 保存?因为我不知道如何保存它。
    • 查看我的编辑。 (我在 XCode 外面输入这个,如果有错误请告诉我。)
    • viewForAnnotation 中的 if/else 无法找到我的财产(if ([pinView.mine boolValue]))
    • 添加 PlacePin *placepin = (PlacePin *)annotation; 解决了我的问题,谢谢!
    • 很高兴你知道那条线,我忘了。请修改我添加的第二行。
    猜你喜欢
    • 2022-08-17
    • 2014-12-20
    • 2014-07-23
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    相关资源
    最近更新 更多