Skip to content

Instantly share code, notes, and snippets.

@imack
Last active August 29, 2015 14:05
Show Gist options
  • Save imack/d9061a1142cfd41feec9 to your computer and use it in GitHub Desktop.
Save imack/d9061a1142cfd41feec9 to your computer and use it in GitHub Desktop.
A Brief Overview of MKAnnotation protocol
//
// PlaceMark.h
// Nina
//
// Created by Ian MacKinnon on 11-08-22.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "Perspective.h"
#import <CoreLocation/CoreLocation.h>
@interface PlaceMark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
Place *_place;
int tag;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
-(id)initWithPlace:(Place*) place;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@property(nonatomic, retain) Place *place;
@property(nonatomic, assign) int tag;
@end
//
// PlaceMark.m
// Nina
//
// Created by Ian MacKinnon on 11-08-22.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "PlaceMark.h"
@implementation PlaceMark
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize place=_place;
@synthesize tag;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
DLog(@"%f,%f",c.latitude,c.longitude);
self.tag = 0;
return self;
}
-(id)initWithPlace:(Place *)place{
if(self = [super init]){
self.title = place.name;
self.subtitle = [place tagString];
//self.subtitle = place.address;
coordinate = place.location.coordinate;
self.place = place;
}
self.tag = 0;
return self;
}
-(void)dealloc{
[title release];
[subtitle release];
[_place release];
[super dealloc];
}
@end
for (Place *place in self.places){
PlaceMark *placemark=[[PlaceMark alloc] initWithPlace:place];
[self.mapView addAnnotation:placemark];
}
.....
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)_annotation{
if( [_annotation isKindOfClass:[PlaceMark class]] ){
PlaceMark *annotation = _annotation;
// try to dequeue an existing pin view first
static NSString* annotationIdentifier = @"placeAnnotationIdentifier";
MKAnnotationView* pinView = (MKAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView) {
// if an existing pin view was not available, create one
pinView = [[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:annotationIdentifier] autorelease];
} else {
pinView.annotation = annotation;
}
pinView.hidden = false;
pinView.enabled = true;
pinView.canShowCallout = YES;
pinView.calloutOffset = CGPointMake(-7, 0);
pinView.centerOffset = CGPointMake(10, -20);
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.tag = [placeSuperset indexOfObjectIdenticalTo:annotation.place];
[rightButton addTarget:self action:@selector(showPlaceDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
if (annotation.place.bookmarked){
if ( annotation.place.highlighted ) {
pinView.image = [UIImage imageNamed:@"HilightMarker.png"];
} else {
pinView.image = [UIImage imageNamed:@"MyMarker.png"];
}
} else {
pinView.image = [UIImage imageNamed:@"FriendMarker.png"];
}
if ( userFilter || tagFilter){
annotation.tag = 0;
}else {
annotation.tag = 1;
}
for (Perspective *perspective in annotation.place.placemarks){
if ( (!userFilter || [perspective.user.username isEqualToString:userFilter]) && (!tagFilter || [perspective.tags indexOfObject:tagFilter] != NSNotFound ) ){
pinView.tag =1;
annotation.tag =1;
perspective.hidden = false;
} else {
perspective.hidden = true;
}
}
if (annotation.tag == 1){
annotation.place.hidden = false;
} else {
annotation.place.hidden = true;
}
if (annotation.tag == 1){
return pinView;
}
if ( tagFilter || userFilter ){
pinView.hidden = true;
pinView.enabled = false;
}
pinView.tag = 0;
annotation.tag = 0;
return pinView;
} else {
return nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment