简体   繁体   中英

MKMapView: Issue with changing pin image

I am trying to change the standard pin to my own image but I keep failing after several attempts.

I have tried different codes and guides that I have found here in this forum but none of them seems to work. I belive I am pasting the code in to my project wrongly. Any ideas how to replace the regular pin with my own image?

//.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapPin : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

- (id)initWithLocation:(CLLocationCoordinate2D)coord;

@end

//.m

#import <Foundation/Foundation.h>
#import "MapPin.h"

@implementation MapPin

@synthesize coordinate,title,subtitle;

- (id)initWithLocation:(CLLocationCoordinate2D)coord{

    self = [super init];
    if (self) {
        coordinate = coord;
    }
    return self;
}
@end

//Viewcontroller.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface FirstViewController : UIViewController <UIAlertViewDelegate, UIWebViewDelegate> {

    MKMapView *mapview;

}
- (IBAction)information;
@property (strong, nonatomic) IBOutlet UIScrollView *ScrollView;
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (retain, nonatomic) IBOutlet MKMapView *mapview;

- (IBAction)showMenu;
- (IBAction)setMap:(id)sender;
- (IBAction)GetLocation:(id)sender;

@end

//Viewcontroller.m

#import "FirstViewController.h"
#import "MapPin.h"

@implementation FirstViewController

@synthesize ScrollView, image;
@synthesize mapview;

- (void)viewDidLoad{

    MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
    region.center.latitude = 55.709900;
    region.center.longitude = 13.201207;
    region.span.longitudeDelta = 0.032f;
    region.span.latitudeDelta = 0.032f;
    [mapview setRegion:region animated:YES];

    MapPin *ann = [[MapPin alloc] init];
    ann.title = @"test Town";
    ann.subtitle = @"test Nation";
    ann.coordinate = region.center;
    ann.coordinate = region.center;
    [mapview addAnnotation:ann];

    MKCoordinateRegion region2 = { {0.0, 0.0}, {0.0,0.0}};
    region2.center.latitude = 55.703904;
    region2.center.longitude = 13.201207;
    region2.span.longitudeDelta = 0.032f;
    region2.span.latitudeDelta = 0.032f;
    [mapview setRegion:region2 animated:YES];

    MapPin *ann2 = [[MapPin alloc] init];
    ann2.title = @"test Town";
    ann2.subtitle = @"test Nation";
    ann2.coordinate = region2.center;
    ann2.coordinate = region2.center;
    [mapview addAnnotation:ann2];

    ScrollView.scrollEnabled = YES;
    [ScrollView setContentSize:CGSizeMake(320, 515)];   
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{     
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    pinView.pinColor= MKPinAnnotationColorGreen;

    pinView.enabled = YES;
    pinView.canShowCallout = YES;
    pinView.image=[UIImage imageNamed:@"test.png"]; //here I am giving the image


    return pinView;
}

See my other answer for working implementation.

Setting an IBOutlet delegate:

Since you're using an IBOutlet for your MKMapView you should control-drag from your MKMapView in your storyboard/xib file to the ViewController/"File's Owner" and select "delegate" from the popup.

Here a SO answer that covers creating custom pins: Custom pins

Also,

pinView.image=[UIImage imageNamed:@"test.png"]; 

should be

pinView.image=[UIImage imageNamed:@"test"];

Reference: Image from imageNamed:

The following worked for me:

  1. Make sure MapKit.framework has been added to your project.
  2. Make sure test.png is in your project (preferably in Images.xcassets)
  3. Control-drag from your mapView in your storyboard to the ViewController and connect "delegate".

Then...

#import "MapPin.h"
#import <MapKit/MapKit.h>

@interface ViewController () <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapview;

@end

@implementation ViewController

- (void)viewDidLoad
{
    MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
    region.center.latitude = 55.709900;
    region.center.longitude = 13.201207;
    region.span.longitudeDelta = 0.032f;
    region.span.latitudeDelta = 0.032f;
    [self.mapview setRegion:region animated:YES];

    MapPin *ann = [[MapPin alloc] init];
    ann.title = @"test Town";
    ann.subtitle = @"test Nation";
    ann.coordinate = region.center;
    ann.coordinate = region.center;
    [self.mapview addAnnotation:ann];

    MKCoordinateRegion region2 = { {0.0, 0.0}, {0.0,0.0}};
    region2.center.latitude = 55.703904;
    region2.center.longitude = 13.201207;
    region2.span.longitudeDelta = 0.032f;
    region2.span.latitudeDelta = 0.032f;
    [self.mapview setRegion:region2 animated:YES];

    MapPin *ann2 = [[MapPin alloc] init];
    ann2.title = @"test Town";
    ann2.subtitle = @"test Nation";
    ann2.coordinate = region2.center;
    ann2.coordinate = region2.center;
    [self.mapview addAnnotation:ann2];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView) {
        return annotationView;
    } else {
        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                         reuseIdentifier:AnnotationIdentifier];
        annotationView.image = [UIImage imageNamed:@"test"];
        return annotationView;
    }
}
@end

So I have followed the guidelines above, added the line of code and step3. Control-drag from your mapView in your storyboard to the ViewController and connect "delegate". which I named "pin".

But my image wont appear...I'll paste the new code again..there must be something wrong in it.

//Viewcontroller.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface FirstViewController : UIViewController <UIAlertViewDelegate, UIWebViewDelegate, MKMapViewDelegate> {

    MKMapView *mapview;

}
- (IBAction)information;
@property (strong, nonatomic) IBOutlet UIScrollView *ScrollView;
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (retain, nonatomic) IBOutlet MKMapView *mapview;
@property (strong, nonatomic) IBOutlet MKMapView *pin;  

- (IBAction)showMenu;
- (IBAction)setMap:(id)sender;
- (IBAction)GetLocation:(id)sender;

@end

//Viewcontroller.m

#import "FirstViewController.h"
#import "MapPin.h"

@implementation FirstViewController

@synthesize ScrollView, image;
@synthesize mapview;
@synthesize pin;

- (void)viewDidLoad{

    MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
    region.center.latitude = 55.709900;
    region.center.longitude = 13.201207;
    region.span.longitudeDelta = 0.032f;
    region.span.latitudeDelta = 0.032f;
    [self.mapview setRegion:region animated:YES];

    MapPin *ann = [[MapPin alloc] init];
    ann.title = @"test Town";
    ann.subtitle = @"test Nation";
    ann.coordinate = region.center;
    ann.coordinate = region.center;
    [self.mapview addAnnotation:ann];

    MKCoordinateRegion region2 = { {0.0, 0.0}, {0.0,0.0}};
    region2.center.latitude = 55.703904;
    region2.center.longitude = 13.201207;
    region2.span.longitudeDelta = 0.032f;
    region2.span.latitudeDelta = 0.032f;
    [self.mapview setRegion:region2 animated:YES];

    MapPin *ann2 = [[MapPin alloc] init];
    ann2.title = @"test Town";
    ann2.subtitle = @"test Nation";
    ann2.coordinate = region2.center;
    ann2.coordinate = region2.center;
    [self.mapview addAnnotation:ann2];

    ScrollView.scrollEnabled = YES;
    [ScrollView setContentSize:CGSizeMake(320, 515)];   
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{     
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;
}

static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView) {
    return annotationView;
} else {
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                    reuseIdentifier:AnnotationIdentifier];
    annotationView.image = [UIImage imageNamed:@"test"];
    return annotationView;
}
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM