如何在地图中创建多色针?

时间:2011-10-30 19:14:49

标签: iphone colors annotations maps android-mapview

我的地图需要显示“场地”的红色针脚,以及靠近和周围的“停车场”的蓝色针脚。

问题在于两个引脚都以相同的颜色显示。

代码如下:

我的pinAnnotation类看起来像这样:

#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

这是实施:

#import "mapPinAnnotation.h"

@implementation mapPinAnnotation

@synthesize coordinate;
@synthesize pinType;
@synthesize title, subtitle;

-(id) initWithCoordinate: (CLLocationCoordinate2D) c {
    coordinate = c;
    return self;
}    

@end

这是设置引脚的方法 - 注意我使用了一个“tempPin”变量 - 全局声明 - 所以我可以将该引脚传递给“viewForAnnotation”方法 - 但我认为这就是问题所在:

-(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];

}

最后,这是“viewForAnnotation”方法:

-(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;
}

问题是两个引脚都显示相同的颜色。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

尝试更改:

if (tempPin.pinType == @"VENUE") {

为:

if ([((mapPinAnnotation*)annotation).pinType isEqualToString:@"VENUE"]) {

答案 1 :(得分:0)

if ([tempPin.pinType isEqualToStirng:@"VENUE"]) {
    thePin.pinColor = MKPinAnnotationColorGreen;
    NSLog(@"pin-type = %@", tempPin.pinType);
} else {
    thePin.pinColor = MKPinAnnotationColorPurple;
    NSLog(@"pin-type = %@", tempPin.pinType);
}

Also look at this