我有大约190个自定义引脚的MKMap。我想根据布尔值更改引脚图像。但是我已经制作了静态布尔和in - (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;
}
答案 0 :(得分:1)
在 - (void)Map中,{for循环中的mine
值已更改,并且它注册了循环中最后一项的值(很可能是假的)。
在viewForAnnotation
中,mine
值没有针对每个注释进行更改,因此在显示注释时会全部为false。
您需要将mine
值保存到自定义注释PlacePin
- (无效)每个项目的地图(就像title
和subtitle
)并检索if-else语句的viewForAnnotation
值。
此外,您可能无法在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;
In - (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"];
}
...
}
}