0

I'm using Parse.com as a backend and i want to show Geopoints on my map. Every Geopoint is also connected with a database boolean field true or false.

How can I show a green pins colour for the "true" gepoint and red pins for the "false" Geopoint?

Here is code for the MapViewController.m

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

I have a function to perform the query against the parse.com database to return me all location data. It is called within the viewDidLoad Method.

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

Then I set the annotationView like this:

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

Here is the Code for the MapViewAnnotation.m (Object):

#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

Can anyone give me a hint how I can show green and red pins based on a boolean?

Thanks in advance!

Nazik
  • 8,696
  • 27
  • 77
  • 123
MarcJohnson
  • 692
  • 2
  • 7
  • 22
  • In the MapViewAnnotation class, where is this boolean stored? You can then access that boolean in the viewForAnnotation delegate method and set pinColor accordingly. Unrelated but your current viewForAnnotation method is also ignoring the result of dequeueReusableAnnotationViewWithIdentifier and always creating a new view. –  Oct 23 '13 at 12:45
  • Thanks Anna! Sorry I´ve been stuck on other work. The boolean is stored in the iniWithObects.. i can get it with the following code block: NSNumber *statusBoolean = [NSNumber numberWithBool:YES]; statusBoolean = [_object objectForKey:@"status"]; NSLog(@"%@",statusBoolean); so how can i use it in the vieForAnnotation delegate method? – MarcJohnson Nov 03 '13 at 14:05
  • I assume MapViewAnnotation is your class that implements MKAnnotation? You have `object` as a property in that class. You can access that property (and the status bool inside it) in `viewForAnnotation` by casting the `annotation` parameter as a MapViewAnnotation. See http://stackoverflow.com/questions/10898122/map-view-annotations-with-different-pin-colors and http://stackoverflow.com/questions/5939223/store-data-in-mkannotation for examples. Try it out and, if any issues, update your question with the code you tried. –  Nov 04 '13 at 02:55
  • Anna, that worked for me! Thank you for the links! – MarcJohnson Nov 05 '13 at 18:37

3 Answers3

0

You can customize the pin annotation view,

- (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;
  }
Nazik
  • 8,696
  • 27
  • 77
  • 123
  • I don't recommend comparing floating-point numbers using a direct equality. Using a non-float attribute (also directly attached to the annotation object) like a string, integer, or boolean as the OP requested is preferable. Also, this sample code doesn't handle view re-use correctly (the pinColor is not being set in the case where flagAnnotationView is not nil -- inside the second else). –  Oct 23 '13 at 12:41
0

in your annotation.h file declare a nsstring as below

@property (strong , nonatomic)NSString *pinColour;

and in the implemention file do the below to check for the colour

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

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

and inside the for loop of parse, tag the relevant with to pass wither colour

annotation.pinColour = @"Red";
mugunthan
  • 77
  • 1
  • 10
-1

Can anyone give me a hint how I can show green and red pins based on a boolean?

BOOL colorRed = YES;

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

Is that what you mean?

jbat100
  • 16,757
  • 4
  • 45
  • 70
  • Hello jbat. Yes that is what i mean. But the problem i am facing is that i dont know how to check check for each annotation item wether it is true or false. I can get the boolean data in "initWithObjects" with NSNumber *statusBoolean = [NSNumber numberWithBool:YES]; statusBoolean = [_object objectForKey:@"status"]; NSLog(@"%@",statusBoolean); so how can i use that data in the pinAnnotationView? – MarcJohnson Nov 03 '13 at 14:01