setting up Image in MKAnnotationPinView - iphone

I have the following code inside the delegate:
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)anAnnotation
{
MKPinAnnotationView *pin = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: #"RoutePin"];
if (pin == nil)
{
if ([anAnnotation isKindOfClass:[RouteMapAnnotation class]])
{
RouteMapAnnotation *theAnnotation = (RouteMapAnnotation *)anAnnotation;
if (theAnnotation.identifier == #"routePin")
{
//NSLog(#"TESTING PART III");
MKPinAnnotationView *startAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:anAnnotation reuseIdentifier:#"RoutePin"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
startAnnotationPin.canShowCallout = YES;
startAnnotationPin.animatesDrop = YES;
startAnnotationPin.rightCalloutAccessoryView = rightButton;
startAnnotationPin.pinColor = MKPinAnnotationColorRed;
return startAnnotationPin;
}
else if (theAnnotation.identifier == #"finishPin")
{
NSLog(#"CREATING FINISH FLAG PRIOR");
MKPinAnnotationView *finishAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:anAnnotation reuseIdentifier:#"FinishPin"];
finishAnnotationPin.canShowCallout = NO;
finishAnnotationPin.animatesDrop = YES;
//finishAnnotationPin.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://cdn4.iconfinder.com/data/icons/formula1/f1_png/128/checkered_flag.png"]]];
finishAnnotationPin.image = [UIImage imageNamed:#"flag_finish"];
return finishAnnotationPin;
}
}
}
return nil;
}
However it's not showing the image for the pin on the map. What am I missing??

You should use MKAnnotationView instead of MKPinAnnotationView.
pin annotation is for pins.

Also note that MKPinAnnotationView does offer some additional functionality to a regular MKAnnotationView, such as animating while dragging and the 3d shadow effect. You won't get these if you use MKAnnotationView.
If you want these built-in features, you can create a UIImageView and add it as a subview to your MKPinAnnotationView. This will give you an annotation that looks like whatever you want; but behaves like a pin. I use it to replace the head of the pin with my own images.

Related

MKAnnotationView doesn't show custom pin

I'm implementing a custom pin for a map, the image is in te project's folder but didn't show.
Perhaps (and very likely) I'm doing something wrong.
The map displays and shows the pins (default red pins) but not the custom image nor the UIButtonTypeDetailDisclosure
This is the code I'm using:
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// in case it's the user location, we already have an annotation, so just return nil
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *TheAnnotationIdentifier = #"theAnnotationIdentifier";
MKAnnotationView *shoppeAnnotationView =
[self.mapView dequeueReusableAnnotationViewWithIdentifier:TheAnnotationIdentifier];
if (shoppeAnnotationView == nil)
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:TheAnnotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:#"pin.png"];
annotationView.rightCalloutAccessoryView = [ UIButton buttonWithType:UIButtonTypeDetailDisclosure ];
annotationView.opaque = NO;
return annotationView;
}
return nil;
}
Any hints?
Thank you!

MKMapView custom pins change when I zoom in and out

I'm making an app that uses MKMapView. I add custom pins (with image). And now when I zoom in and then zoom out, pins change back to default (red color).
Here is my code:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString* SFAnnotationIdentifier = #"Kamera";
MKPinAnnotationView* pinView =
(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
if (!pinView)
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier];
annotationView.canShowCallout = NO;
UIImage *flagImage = [UIImage imageNamed:#"pinModer.png"];
CGRect resizeRect;
resizeRect.size = flagImage.size;
resizeRect.size = CGSizeMake(40, 60);
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationView.image = resizedImage;
annotationView.opaque = NO;
UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"kameraNaprejModra.png"]];
annotationView.leftCalloutAccessoryView = sfIconView;
return annotationView;
}
return nil;
}
The code is not handling the case where the dequeue returns a non-nil pinView (meaning it is re-using a previous annotation view).
If pinView is not nil, the method ends up at the last line which returns nil for the annotation view.
When you return nil, the map view draws the default annotation view which is a red pin.
Adjust the code like this:
if (!pinView)
{
//no changes to code inside this if
//...
return annotationView;
}
//add an else part and return pinView instead of nil...
else
{
pinView.annotation = annotation;
}
return pinView;

Multiple annotation color

i have a map with multiple annotation on it. i was able to show the first and the last annotations. i want to give each annotation a different color.
here is my code of how to insert my annotations
if(i<1 || i >object.count-2)
{
MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=DEVNAME;
myAnnotation1.subtitle=it.address;
[mapView addAnnotation:myAnnotation1];
[annotations addObject:myAnnotation1];
}
the if condition is reading the index of the array to only drop the first and last annotation.
and here is how do i drop the pins on the map...
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString* MyAnnotationIdentifier = #"MyAnnotationIdentifier";
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:MyAnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorRed;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
how to different annotations color?
It is of course not going to work as except for the first and last pin, for rest of the pins, the else part of the code will execute.
So there the MKPinAnnotationView *pinView will be nil. So the annotation with no memory allocated, can not change color!!! :-)
You must alloc, init the MKPinAnnotationView *pinView for else part somewhere according to your requirement.
Use MKAnnotationView class and set a custom image for each pin:
MKAnnotationView *customPinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
customPinView.image = [UIImage imageNamed:#"pin1.png"];
.
.
.
instead of using images from your app bundle, you might need to programmatically build your UIImage so you can achieve unique colors, this is up to you.
Furthermore, you can set a value on your annotation to help identify which annotation gets which image. You'll subclass MKAnnotation to add this integer property called pinNumber.
myAnnotation.pinNumber = 2;
customPinView.image = [UIImage imageNamed:[NSString stringWithFormat:#"pin%d.png",annotation.pinNumber]];

how to make custom pin like friends map of 4sq app

How to make custom pin like foursquare friends map instead of pin on map?
image is here, http://www.socialmedianews.com.au/wp-content/uploads/2010/07/foursquare-friends-map.png
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pinView"];
if (!pinView) {
pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pinView"] autorelease];
pinView.image = [UIImage imageNamed:#"SPOON4.png"];
pinView.frame = CGRectMake(-30, 0, 70, 67.5);
//pinView.animatesDrop = YES; can't animate with custom pin images
pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
} else {
pinView.annotation = annotation;
}
if (annotation == mapView.userLocation){
return nil; //default to blue dot
}
return pinView;
}
You need to use a custom MKAnnotationView.
You can use the MKAnnotationView class
as is or subclass it to provide custom
behavior as needed. The image property
of the class lets you set the
appearance of the annotation view
without subclassing directly. You
might also create custom subclasses as
a convenience and use them to put the
annotation view in a known state. For
example, the MKPinAnnotationView
subclass initializes the contents of
the annotation view to a pin image.
You want to replace the custom pin with a framed portrait. You can draw the frame with Quartz, or add the frame as a second transparent image. I would do the second adding the following to MKAnnotationView:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation
reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil) {
self.opaque = NO;
self.frame = CGRectMake(0,0, self.portraitImage.size.width, self.portraitImage.size.height);
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self.frameImage drawInRect:CGRectMake(0, 0, frameImage.width, frameImage.height)];
[self.portraitImage drawInRect:CGRectMake(0, 0, portraitImage.width, portraitImage.height)];
}

Custom annotationView images revert to pins when clicked

I'm displaying custom images on a map (instead of the default pins) using the code below. However, when I tap on an item (and the callout appears), the image reverts to the default red pin. How can I keep my custom image, even when the callout is displayed?
- (MKAnnotationView *) mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if (annotation != mapView.userLocation)
{
static NSString *pinID = #"mapPin";
pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
if (pinAnnotation == nil)
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease];
// Set the image
pinAnnotation.image = [UIImage imageNamed:#"TestIcon.png"];
// Set ability to show callout
pinAnnotation.canShowCallout = YES;
// Set up the disclosure button on the right side
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = infoButton;
[pinID release];
}
return pinAnnotation;
[pinAnnotation release];
}
I found by making the pinAnnotation a MKAnnotationView rather than a MKPinAnnotationView, I got the desired result. Image doesn't turn into a pin anymore
static NSString *pinID = #"mapPin";
pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
if (pinAnnotation == nil)
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease];
// Set the image
pinAnnotation.image = [UIImage imageNamed:#"TestIcon.png"];
You need to use a subview rather than the image property. This code succssfully solves the problem for me:
UIImage * image = [UIImage imageNamed:#"blue_pin.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[annView addSubview:imageView];