【发布时间】: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 <MKAnnotation>)annotation)。 -
虽然您应该确保地图视图的代理出口在情节提要中连接到视图控制器,但真正的问题是 viewForAnnotation 中的代码正在创建一个 MKPinAnnotationView(倾向于忽略自定义图像)而不是普通的 MKAnnotationView(用于自定义图像/视图)。
-
所以我将 MKMapViewDelegate 添加到 [@interface] 现在看起来像这样 [@interface FirstViewController : UIViewController
{] 并将 [mapview.delegate = self;] 放入查看didload。但这仍然不能解决问题。
标签: ios mkmapview mkannotationview