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

发布于 2024-12-12 13:52:04 字数 3070 浏览 3 评论 0原文

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

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

代码如下:

我的 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;
}

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

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-12-19 13:52:05

尝试将:更改

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

为:

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

Try changing:

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

to:

if ([((mapPinAnnotation*)annotation).pinType isEqualToString:@"VENUE"]) {
表情可笑 2024-12-19 13:52:05
if ([tempPin.pinType isEqualToStirng:@"VENUE"]) {
    thePin.pinColor = MKPinAnnotationColorGreen;
    NSLog(@"pin-type = %@", tempPin.pinType);
} else {
    thePin.pinColor = MKPinAnnotationColorPurple;
    NSLog(@"pin-type = %@", tempPin.pinType);
}

也看看这个

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文