Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created October 14, 2010 08:49
Show Gist options
  • Save marshluca/625898 to your computer and use it in GitHub Desktop.
Save marshluca/625898 to your computer and use it in GitHub Desktop.
#pragma mark -
#pragma mark MKMapViewDelegate
- (void)showDetails:(id)sender
{
[self.navigationController setToolbarHidden:YES animated:NO];
// navigation to detail view
}
- (UIImage *)resizeImage:(UIImage *)flagImage
{
CGRect resizeRect;
resizeRect.size = flagImage.size;
// AnnotationPadding: 10.0f, CalloutHeight:40.0f
CGSize maxSize = CGRectInset(self.view.bounds,10.0f,10.0f).size;
maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0f;
if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[MyAnnotation class]])
{
static NSString* MyIdentifier = @"MyAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:MyIdentifier];
if (!pinView)
{
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:MyIdentifier] autorelease];
// ignore the animatesDrop when custom an image
customPinView.animatesDrop = YES;
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.canShowCallout = YES;
// click button in annotation
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
// image for point on map
UIImage *resizedImage = [self resizeImage:[UIImage imageNamed:@"flag.png"]];
customPinView.image = resizedImage;
customPinView.opaque = NO;
// image for point in annotation
UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"location.png"]];
customPinView.leftCalloutAccessoryView = sfIconView;
[sfIconView release];
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
#import "MyAnnotation.h"
@implementation MyAnnotation
- (id)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
mydict = dict;
}
return self;
}
- (CLLocationCoordinate2D)coordinate;
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [[mydict objectForKey:@"lat"] floatValue];
theCoordinate.longitude = [[mydict objectForKey:@"lng"] floatValue];
return theCoordinate;
}
// required if you set the MKPinAnnotationView's "canShowCallout" property to YES
- (NSString *)title
{
return [mydict objectForKey:@"address"];
}
// optional
- (NSString *)subtitle
{
return [mydict objectForKey:@"full_name"];
}
- (void)dealloc
{
[super dealloc];
}
@end
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject <MKAnnotation>
{
NSDictionary *mydict;
}
- (id)initWithDict:(NSDictionary *)dict;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment