0

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

如何为“真”地理点显示绿色针脚颜色,为“假”地理点显示红色针脚颜色?

这是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

谁能给我一个提示,我如何根据布尔值显示绿色和红色引脚?

提前致谢!

4

3 回答 3

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

并在 parse 的 for 循环中,将相关标记为传递枯萎颜色

annotation.pinColour = @"Red";
于 2014-07-30T06:34:02.750 回答
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;
  }
于 2013-10-22T15:13:57.260 回答
-1

谁能给我一个提示,我如何根据布尔值显示绿色和红色引脚?

BOOL colorRed = YES;

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

你是这个意思吗?

于 2013-10-22T15:08:10.463 回答