My application displays a table view that contains the result of a user search from a database. Now in this tabular view there is one button by clicking on this user to see the location of all the users who are searching on the map. When the user first clicks on the button, he will be able to see the entire user's location with a green color. I show the map in half of the review. Now that the user selects a specific user from the table, I want to change the color of this user pin. And if the user selects any other user from the table, the previous selected icon color of the user must be changed as it was before. I tried this:
- (void)selectAnnotation:(id < MKAnnotation >)annotation animated:(BOOL)animated{
MKPinAnnotationView *chng=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
chng.pinColor=MKPinAnnotationColorRed;
NSLog(@"========>selected");
}
And I call this method when the user selects a specific user from the table.
CLLocationCoordinate2D CurrentCoordinateSingleUser;
CurrentCoordinateSingleUser.latitude=[[singleUserPin objectAtIndex:1] doubleValue];
CurrentCoordinateSingleUser.longitude=[[singleUserPin objectAtIndex:2] doubleValue];
MapObjects *map1=[[MapObjects alloc]initwithCoordinate:CurrentCoordinateSingleUser title:[singleUserPin objectAtIndex:0] subtitle:Nil];
[userMap addAnnotation:map1];
for (id<MKAnnotation> currentAnnotation in userMap.annotations) {
if ([currentAnnotation isEqual:annotationToSelect]) {
[userMap selectAnnotation:currentAnnotation animated:FALSE];
}
}
But it selectAnnotationdoesn’t work here .
UPDATE ::
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *identifier = @"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[userMap dequeueReusableAnnotationViewWithIdentifier:identifier];
if([annotation isEqual:map1]){
annotationView.pinColor = MKPinAnnotationColorRed;
}
else {
if (!annotationView)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.pinColor = MKPinAnnotationColorGreen;
annotationView.animatesDrop = NO;
annotationView.canShowCallout = YES;
}
else {
annotationView.annotation = annotation;
}
}
return annotationView;
}
MapObjects.h:
@interface MapObjects : NSObject<MKMapViewDelegate,MKAnnotation>
{
NSString *title,*subtitle;
CLLocationCoordinate2D coordinate;
}
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *subtitle;
@property(nonatomic) CLLocationCoordinate2D coordinate;
-(MapObjects *)initwithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
@end
source
share