Ios MKMapView:更改pin图像时出现问题
我正在尝试将标准pin更改为我自己的图像,但几次尝试后我一直失败 我尝试过在这个论坛上找到的不同的代码和指南,但它们似乎都不起作用。我相信我将代码粘贴到我的项目中是错误的。有没有办法用我自己的图像替换普通的pin码 /.hIos MKMapView:更改pin图像时出现问题,ios,mkmapview,mkannotationview,Ios,Mkmapview,Mkannotationview,我正在尝试将标准pin更改为我自己的图像,但几次尝试后我一直失败 我尝试过在这个论坛上找到的不同的代码和指南,但它们似乎都不起作用。我相信我将代码粘贴到我的项目中是错误的。有没有办法用我自己的图像替换普通的pin码 /.h #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MapPin : NSObject <MKAnnotation> { CLLocati
#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
#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
#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
#导入
#进口
@接口MapPin:NSObject{
CLLocationCoordinate2D坐标;
NSString*标题;
NSString*副标题;
}
@属性(非原子,赋值)CLLocationCoordinate2D坐标;
@属性(非原子,副本)NSString*标题;
@属性(非原子,复制)NSString*副标题;
-(id)initWithLocation:(CLLocationCoordinate2D)coord;
@结束
/.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
#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;
}
#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
#导入
#导入“MapPin.h”
@实现MapPin
@综合坐标、标题、副标题;
-(id)initWithLocation:(CLLocationCoordinate2D)coord{
self=[super init];
如果(自我){
坐标=坐标;
}
回归自我;
}
@结束
//Viewcontroller.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
#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
#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
#导入
#进口
@界面FirstViewController:UIViewController{
MKMapView*mapview;
}
-(i)信息;
@属性(强,非原子)IBUIScrollView*ScrollView;
@属性(强,非原子)IBUIImageView*图像;
@属性(保留,非原子)IBMOutlet MKMapView*mapview;
-(iAction)显示菜单;
-(iAction)setMap:(id)发送方;
-(iAction)GetLocation:(id)发送方;
@结束
//Viewcontroller.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
#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;
}
#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
#导入“FirstViewController.h”
#导入“MapPin.h”
@FirstViewController的实现
@综合视图、图像;
@综合地图视图;
-(无效)viewDidLoad{
MKCoordinateRegion={0.0,0.0},{0.0,0.0};
region.center.lation=55.709900;
region.center.longitude=13.201207;
region.span.longitudeDelta=0.032f;
region.span.latitudeDelta=0.032f;
[地图视图设置区域:区域动画:是];
MapPin*ann=[[MapPin alloc]init];
ann.title=@“测试城市”;
ann.subtitle=@“测试国家”;
ann.coordinate=region.center;
ann.coordinate=region.center;
[地图视图添加注释:ann];
MkCoordinateRegion2={{0.0,0.0},{0.0,0.0};
region2.center.lation=55.703904;
region2.center.longitude=13.201207;
区域2.span.longitudeDelta=0.032f;
区域2.span.latitudeDelta=0.032f;
[地图视图设置区域:区域2动画:是];
MapPin*ann2=[[MapPin alloc]init];
ann2.title=@“试验城镇”;
ann2.subtitle=@“测试国家”;
ann2.坐标=区域2.中心;
ann2.坐标=区域2.中心;
[地图视图添加注释:ann2];
ScrollView.scrollEnabled=是;
[ScrollView setContentSize:CGSizeMake(320515)];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
静态NSString*AnnotationIdentifier=@“AnnotationIdentifier”;
MKPinAnnotationView*pinView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation重用标识符:AnnotationIdentifier];
pinView.animatesDrop=是;
pinView.canShowCallout=是;
pinView.pinColor=MKPinAnnotationColorGreen;
pinView.enabled=是;
pinView.canShowCallout=是;
pinView.image=[UIImage ImageName:@“test.png”];//这里我给出了这个图像
返回pinView;
}
请参阅我关于工作实现的其他答案。
设置IBOutlet委托:
由于您正在为MKMapView使用IBOutlet,您应该控制从序列图像板/xib文件中的MKMapView到ViewController/“文件所有者”的拖动,并从弹出窗口中选择“委派”
下面是一个涵盖创建自定义PIN的SO答案:
而且
应该是
pinView.image=[UIImage imageNamed:@"test"];
参考资料:以下内容对我很有用:
#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
#导入“MapPin.h”
#进口
@界面视图控制器()
@属性(弱,非原子)IBMMapView*mapview;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
MKCoordinateRegion={0.0,0.0},{0.0,0.0};
region.center.lation=55.709900;
region.center.longitude=13.201207;
region.span.longitudeDelta=0.032f;
region.span.latitudeDelta=0.032f;
[self.mapview setRegion:区域动画:是];
MapPin*ann=[[MapPin alloc]init];
ann.title=@“测试城市”;
ann.subtitle=@“测试国家”;
ann.coordinate=region.center;
ann.coordinate=region.center;
[self.mapview addAnnotation:ann];
MkCoordinateRegion2={{0.0,0.0},{0.0,0.0};
region2.center.lation=55.703904;
region2.center.longitude=13.201207;
区域2.span.longitudeDelta=0.032f;
区域2.span.latitudeDelta=0.032f;
[self.mapview setRegion:region2动画:是];
MapPin*ann2=[[MapPin alloc]init];
ann2.title=@“试验城镇”;
ann2.subtitle=@“测试国家”;
ann2.坐标=区域2.中心;
ann2.坐标=区域2.中心;
[self.mapview addAnnotation:ann2];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
if([annotation isKindOfClass:[MKUserLocation类]]){
返回零;
}
静态NSString*AnnotationIdentifier=@“AnnotationIdentifier”;
MKAnnotationView*annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
如果(注释视图){
返回注释视图;
}否则{
MKAnnotationView*annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier