【问题标题】:MKMapView: Issue with changing pin imageMKMapView:更改图钉图像的问题
【发布时间】:2015-04-28 17:35:43
【问题描述】:

我正在尝试将标准图钉更改为我自己的图像,但多次尝试后仍然失败。

我尝试了在此论坛中找到的不同代码和指南,但它们似乎都不起作用。我相信我错误地将代码粘贴到我的项目中。任何想法如何用我自己的图像替换常规图钉?

//.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;
}

【问题讨论】:

  • 我认为你需要在@interface...FirstViewController 声明中添加MKMapViewDelegate,例如在viewDidLoad 中设置mapview.delegate = self。如果没有这两行,则不会调用其他方法 (-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation)。
  • 虽然您应该确保地图视图的代理出口在情节提要中连接到视图控制器,但真正的问题是 viewForAnnotation 中的代码正在创建一个 MKPinAnnotationView(倾向于忽略自定义图像)而不是普通的 MKAnnotationView(用于自定义图像/视图)。
  • 所以我将 MKMapViewDelegate 添加到 [@interface] 现在看起来像这样 [@interface FirstViewController : UIViewController {] 并将 [mapview.delegate = self;] 放入查看didload。但这仍然不能解决问题。

标签: ios mkmapview mkannotationview


【解决方案1】:

请参阅我的其他工作实施答案。

设置 IBOutlet 委托:

由于您为 MKMapView 使用 IBOutlet,您应该控制并从 storyboard/xib 文件中的 MKMapView 拖动到 ViewController/“文件所有者”,然后从弹出窗口中选择“委托”。

这是一个涵盖创建自定义引脚的 SO 答案:Custom pins

还有,

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

应该是

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

参考:Image from imageNamed:

【讨论】:

【解决方案2】:

以下内容对我有用:

  1. 确保已将 MapKit.framework 添加到您的项目中。
  2. 确保 test.png 在您的项目中(最好在 Images.xcassets 中)
  3. 从情节提要中的 mapView 按住 Control 并拖动到 ViewController 并连接“delegate”。

那么……

#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

【讨论】:

    【解决方案3】:

    所以我遵循了上面的指导方针,添加了代码行和第 3 步。从情节提要中的 mapView 按住 Control 并拖动到 ViewController 并连接“delegate”。我将其命名为“pin”。

    但是我的图像不会出现...我将再次粘贴新代码..其中一定有问题。

    //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
    

    【讨论】:

    • 任何想法@WilliamSaults
    • 我通过添加 mapView.delegate = self; 解决了这个问题进入viewdidload
    • 1) 您不要为代表创建单独的出口。您将地图视图上的委托出口连接到 vc(因此地图视图将有两个连接到视图控制器:它自己和它的委托)。在情节提要中连接删除出口的替代方法 是像您一样在代码中执行此操作。 2) 你可能想从一个更简单的项目开始学习 iOS。 3)您应该使用此更新的代码更新您的问题,而不是将其作为答案发布。参加 SO 之旅,了解这里的工作原理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多