I want to show two flags on Map but these are not pins. I search a lot for this but could not find solutions but to add as pin.
please help
you can do it with MKAnnotationView
with image
property like bellow in viewForAnnotation:
method..
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"Current";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
if (annotation == mapView.userLocation)
return nil;
annotationView.image = [UIImage imageNamed:@"yourImageName.png"];
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
return annotationView;
}
You are looking for Map Overlay MKOverlayView
.
Check these tutorials:
Creating overlay
MKOverlayView
Create a subclass of MKOverlayView
like:
.h #import #import
@interface MapOverlayView : MKOverlayView
{
}
@end
.m
#import "MapOverlayView.h"
@implementation MapOverlayView
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx
{
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextDrawImage(ctx, theRect, imageReference);
}
@end
Implement the viewForOverlay:
,inside that create the overlay and add to map.
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MapOverlay *mapOverlay = (MapOverlay *)overlay;
MapOverlayView *mapOverlayView = [[[MapOverlayView alloc] initWithOverlay:mapOverlay] autorelease];
return mapOverlayView;
}
try this one
annotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"try"];
annotation.canShowCallout = YES;
annotation.image = [UIImage imageNamed:@"image.png"];
return annotation;
and do not use annotation.animatesDrop property.
If you are talkin about MKMapView
:
To show an image instead of Pin annotation , you need to override MKMapView's method
- (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation
Like this:
- (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation{
static NSString* annotationIdentifier = @"Identifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
annotationView.canShowCallout = YES;
// here you need to give the image you want instead of pin
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"balloon.png"]];
return annotationView;
}
return nil;
}