Reputation: 137
I have a Table view that has to push into map. I am trying to get the title from the annotation to show in the map view and I need to compare the value from subtitle value to show in map exactly based on annotation value, but I am getting in map is title and subtitle value. I don't want to show the subtitle but i want it to compare the values Here is my code:
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKAnnotationView *pinView = nil;
if(annotation != mapview.userLocation)
{
NSLog(@"%i",k);
static NSString *defaultPinID = @"defaultPinID";
pinView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
NSString *st=@"0";
NSString *str = [annotation subtitle];
if ([str isEqualToString:st]) {
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.image=[UIImage imageNamed:@"greyDot.png"];
//return pinView;
}
NSString *st1=@"10";
if ([str isEqualToString:st1]) {
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.image=[UIImage imageNamed:@"blackDot.png"];
//return pinView;
}
NSString *st2=@"20";
if ([str isEqualToString:st2]) {
//pinView.pinColor = MKPinAnnotationColorPurple;
pinView.image=[UIImage imageNamed:@"greendot.png"];
//return pinView;
}
NSString *st3=@"30";
if ([str isEqualToString:st3]) {
//pinView.pinColor = MKPinAnnotationColorRed;
pinView.image=[UIImage imageNamed:@"blueDot.png"];
//return pinView;
}
pinView.canShowCallout = YES;
infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Display *t=annotation;
infoButton.tag=k;
[infoButton addTarget:self action:@selector(pinLabelClicked:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = infoButton;
}
else {
[mapview.userLocation setTitle:@"I am here"];
}
return pinView;
//k++;
}
this is my code for generate annotations
- (void)generateAnnotations
{
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
[mapview setDelegate:self];
for( int i=0;i<[addressArray count];i++ ){
AddressInfo *address = [addressArray objectAtIndex:i];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [address.latitude floatValue];
region.center.longitude = [address.longitude floatValue];
region.span.latitudeDelta=10.01f;
region.span.longitudeDelta=10.01f;
[mapview setRegion:region animated:YES];
//[mapview setRegion:region animated:TRUE];
//[mapview regionThatFits:region];
DisplayMap *ann = [[DisplayMap alloc] init];
//ann.tt=i;
k=i;
ann.title = address.name;
ann.subtitle = [NSString stringWithFormat:@"%@",address.category];
ann.sa = address.streetaddress;
ann.sb = address.suburb;
ann.ph = address.phoneNo;
ann.index = i;
//ann.image = [NSString stringWithFormat@"image_%d.png",i];
ann.coordinate=region.center;
[mapview addAnnotation:ann];
[ann release];
}
}
what I am doing here is I am comparing category value based on this value I need to show pins in map view with relative color.. any one have idea how to do this?
Upvotes: 1
Views: 670
Reputation:
As explained in this previous question:
how to make the annotation subtitle disappear from title menu?
you need to leave subtitle
blank and use a separate custom property in your annotation class to store your category.
That way, the subtitle
will not display in the callout (since it will be blank) but you will still be able to get the annotation's category to make decisions.
First, in your annotation class .h (which would be DisplayMap.h), add a property for category
:
@property (nonatomic, copy) NSString *category;
Next, in Display.m, synthesize the property:
@synthesize category;
Next, in generateAnnotations
, where you currently set the annotation's subtitle
, set the category
instead and leave subtitle
blank (don't set it):
ann.category = [NSString stringWithFormat:@"%@",address.category];
Finally, in viewForAnnotation
, you can check the annotation's category
. To do so, first check if the annotation is of type DisplayMap
and then cast it.
So replace this part:
NSString *str = [annotation subtitle];
//code that sets pinView.image based on value of str...
with this:
pinView.image = set_to_some_default_image;
if ([annotation isKindOfClass:[DisplayMap class]])
{
DisplayMap *dmAnn = (DisplayMap *)annotation;
NSString *str = dmAnn.category;
//code that sets pinView.image based on value of str...
}
A completely separate issue is that it looks like you're using a button tag and a custom method to handle the callout button press. In the custom method, you may be trying to get the data of the annotation that was tapped. I very strongly do not recommend using a button tag or a custom method. For much better alternatives, see the following:
Upvotes: 1