Issue
My map needs to display RED pins for "Venues", and BLUE pins for "Parking Lots" near and around them.
The problem is that both pins show up in the same color.
The code is below:
My pinAnnotation class looks like this:
#import <MapKit/MapKit.h>
@interface mapPinAnnotation : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
NSString *pinType; // this string will be set to either "Venue" or "Parking"
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, retain) NSString *title, *subtitle;
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *pinType;
@end
This is the implementation:
#import "mapPinAnnotation.h"
@implementation mapPinAnnotation
@synthesize coordinate;
@synthesize pinType;
@synthesize title, subtitle;
-(id) initWithCoordinate: (CLLocationCoordinate2D) c {
coordinate = c;
return self;
}
@end
Here is the method setting the pins - note that I use a "tempPin" variable - declared globally - so I can then pass that pin into the "viewForAnnotation" method - but I think this is where the problem is:
-(void) dropThePin {
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
location.latitude = latitude;
location.longitude = longitude;
if(pinAnnotation != nil) {
[mapView removeAnnotation:pinAnnotation];
[pinAnnotation release];
pinAnnotation = nil;
}
// Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it:
pinAnnotation = [[mapPinAnnotation alloc] initWithCoordinate:location];
pinAnnotation.pinType = @"VENUE";
tempPin = pinAnnotation;
[pinAnnotation setTitle: @"Some Stadium"];
[pinAnnotation setSubtitle: @"123 Main St."];
[mapView addAnnotation:pinAnnotation];
// Set-Up of 2nd. Pin:
location.latitude = 12.34567;
location.longitude = -23.45678;
pinAnnotation.pinType = @"PARKING";
if(parkingLotPin != nil) {
[mapView removeAnnotation:parkingLotPin];
[parkingLotPin release];
parkingLotPin = nil;
}
// Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it:
parkingLotPin = [[mapPinAnnotation alloc] initWithCoordinate:location];
tempPin = pinAnnotation;
[parkingLotPin setTitle: @"Another Venue"];
[parkingLotPin setSubtitle: @"789 S. Broad Street"];
[mapView addAnnotation:parkingLotPin];
[parkingLotPin release];
}
Finally, here is the "viewForAnnotation" method:
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *thePin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
if (tempPin.pinType == @"VENUE") {
thePin.pinColor = MKPinAnnotationColorGreen;
NSLog(@"pin-type = %@", tempPin.pinType);
} else {
thePin.pinColor = MKPinAnnotationColorPurple;
NSLog(@"pin-type = %@", tempPin.pinType);
}
thePin.animatesDrop=TRUE;
thePin.canShowCallout = YES;
thePin.calloutOffset = CGPointMake(-5, 5);
return thePin;
}
The problem is that both pins show up with the same color.
Any suggestions?
Solution
Try changing:
if (tempPin.pinType == @"VENUE") {
to:
if ([((mapPinAnnotation*)annotation).pinType isEqualToString:@"VENUE"]) {
Answered By - Joe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.