MKAnnotationView - 设置两种不同的Pin颜色

时间:2013-10-22 14:48:47

标签: ios objective-c cocoa-touch mkmapview mkannotation

我正在使用Parse.com作为后端,我想在地图上显示Geopoints。 每个Geopoint也与数据库布尔字段true或false连接。

如何为“真实”gepoint发出绿色针脚颜色,为“假”Geopoint显示红色针脚?

以下是 MapViewController.m

的代码
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

我有一个函数来对parse.com数据库执行查询以返回所有位置数据。它在viewDidLoad方法中调用。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getAllStations];
}

然后我像这样设置annotationView:

#pragma mark - MapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)geoPointAnnotation {
    static NSString *MapViewAnnotationIdentifier = @"Places";

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:MapViewAnnotationIdentifier];


    if (geoPointAnnotation == mapView.userLocation) {
        return nil;

    } else {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:geoPointAnnotation reuseIdentifier:MapViewAnnotationIdentifier];
        annotationView.pinColor = MKPinAnnotationColorGreen;
        annotationView.canShowCallout = YES;
        annotationView.draggable = YES;
        annotationView.animatesDrop = YES;
    }

    return annotationView;
}

以下是 MapViewAnnotation.m(对象)的代码:

#import "MapViewAnnotation.h"
#import "Parse/Parse.h"

@interface MapViewAnnotation ()

@property (nonatomic, strong) PFObject *object;

@end


@implementation MapViewAnnotation


#pragma mark - Initialization

- (id)initWithObject:(PFObject *)aObject {
    self = [super init];
    if (self) {
        _object = aObject;

        PFGeoPoint *geoPoint = self.object[@"location"];
        [self setGeoPoint:geoPoint];        }
    return self;
}


- (void)setGeoPoint:(PFGeoPoint *)geoPoint {
    _coordinate = CLLocationCoordinate2DMake(geoPoint.latitude, geoPoint.longitude);

        NSString *streetName = self.object[@"adress"];

    _title = [NSString stringWithFormat:@"%@", [_object objectForKey:@"name"]];


    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *currentLocationGeoPoint, NSError *error) { //Get current Location
        if (!error) {

            PFGeoPoint *distanceGeoPoint = [_object objectForKey:@"location"];

            double distanceDouble  = [currentLocationGeoPoint distanceInKilometersTo:distanceGeoPoint];
            //NSLog(@"Distance: %.1f",distanceDouble);

            _subtitle = [NSString stringWithFormat:@"%@ - Distance: %.1f km", streetName, distanceDouble]; 
        }
    }];
}
@end

任何人都可以给我一个提示,告诉我如何根据布尔值显示绿色和红色引脚吗?

提前致谢!

3 个答案:

答案 0 :(得分:0)

您可以自定义图钉注释视图

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }



    MKAnnotationView *flagAnnotationView =
    [self.mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
    if (flagAnnotationView == nil)
    {
        MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
                                              initWithAnnotation:annotation reuseIdentifier:SFAnnotationIdentifier];

    if(annotation.coordinate.latitude==42.0000 && annotation.coordinate.longitude==-87.65000)
    //you have to keep the latitude and longitude of the pins to which you can set the colour of the pin according to that latlong
        customPinView.pinColor = MKPinAnnotationColorGreen;
    else
         customPinView.pinColor = MKPinAnnotationColorRed;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;
        return customPinView;
     }
      else
       {
        flagAnnotationView.annotation = annotation;
       }
      return flagAnnotationView;

      return nil;
  }

答案 1 :(得分:0)

你的annotation.h文件中的

声明了一个nsstring,如下所示

@property (strong , nonatomic)NSString *pinColour;

并在实施文件中执行以下操作以检查颜色

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{

//other code above
if ([[annotation pinColour] isEqualToString:@"Red"]) {
        annotationView.pinColor = MKPinAnnotationColorRed;
    }else{
        annotationView.pinColor = MKPinAnnotationColorGreen;
    }
}

并在解析的for循环内,标记相关的以传递枯萎颜色

annotation.pinColour = @"Red";

答案 2 :(得分:-1)

  

任何人都可以给我一个提示,告诉我如何根据布尔值显示绿色和红色引脚吗?

BOOL colorRed = YES;

if (colorRed) annotationView.pinColor = MKPinAnnotationColorRed;
else annotationView.pinColor = MKPinAnnotationColorGreen;

这是你的意思吗?