-6

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

Azhar
  • 20,500
  • 38
  • 146
  • 211

4 Answers4

1

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;
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • where to set the image location? – Azhar Jul 29 '13 at 11:33
  • when you add any location with lat-long in your class at that time this delegate method called automatically and this image drop with that lat-log on that location .. – Paras Joshi Jul 29 '13 at 11:36
  • @NG_SE see demo example for custom pin on mkmapview from this link http://iphoneapp-dev.blogspot.in/2011/01/how-to-place-animated-annotations-on.html – Paras Joshi Jul 29 '13 at 11:38
1

You are looking for Map Overlay MKOverlayView.

Check these tutorials:

Creating overlay

  • Creating a 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
  • Adding Overlay

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;
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

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.

Agent Chocks.
  • 1,312
  • 8
  • 19
0

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;
        }
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33