在我的MKMap视图中,我使用图像自定义了注释引脚。但是仍然有些引脚是静态的,没有显示给定的图像。
我正在使用 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation 来设置图片图片。
在此处添加我的代码和屏幕:
-(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:@"bublerest.png"]; //here I am giving the image
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rest_image2.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
return pinView;
}
有什么想法吗?
答案 0 :(得分:12)
如果您希望MKAnnotationView
上的图片与苹果“Pin”不同,则必须使用MKAnnotationView
代替MKPinAnnotationView
。
MKPinAnnotationView不是以这种方式定制的,并且会不时地显示Pin。
Class Reference表示只应更改pinColor
和animatesDrop
。
但是你会丢失PinAnnotationView的动画和阴影。
答案 1 :(得分:2)
在MKAnnotation
协议中创建一个属性,用于设置引脚,例如
@interface AddressAnnotation1 : NSObject<MKAnnotation> {
}
@property (nonatomic, retain) NSString *mPinColor;
在.m文件实现文件
中- (NSString *)pincolor{
return mPinColor;
}
- (void) setpincolor:(NSString*) String1{
mPinColor = String1;
}
添加annotation
时,也设置pin
颜色。
#pragma mark annotation delegate
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(AddressAnnotation1 *) annotation
{
UIImage *anImage = nil;
MKAnnotationView *annView=(MKAnnotationView*)[mapView1 dequeueReusableAnnotationViewWithIdentifier:@"annotation"];
if(annView==nil){
annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"] autorelease];
}
if([annotation.mPinColor isEqualToString:@"green"])
{
anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]];
}
else if([annotation.mPinColor isEqualToString:@"red"])
{
anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 01.png" ofType:nil]];
}
else if([annotation.mPinColor isEqualToString:@"blue"])
{
anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]];
}
annView.image = anImage;
return annView;
}
如果您不希望针脚的自定义图像用
替换条件语句的主体annView.pinColor = MKPinAnnotationColorGreen;
或
annView.pinColor = MKPinAnnotationColorRed;
或
annView.pinColor = MKPinAnnotationColorPurple;
我之前已经实现了这一点。所以请坚持下去。
答案 2 :(得分:1)
发布我在最近的项目中使用的示例代码段。希望这会有所帮助。
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation{
Annotation *annotationInst = (Annotation*)annotation;
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"pinId";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ){
pinView = [[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
}
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:LOCATION_BUBBLE];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
}
return pinView;
}
答案 3 :(得分:1)
在viewForAnnotation
if ([annotation isKindOfClass:[MyAnnotation class]]) {
// try to dequeue an existing pin view first
static NSString* myAnnotationIdentifier = @"MyAnnotationIdentifier";
// If an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myAnnotationIdentifier];
if ([(MyAnnotation *)annotation ann] == AnnotationTypeStart) {
NSLog(@"In Start");
customPinView.enabled = NO;
customPinView.image = [UIImage imageNamed:@"begin.png"];
}
}
其中MyAnnotation
的实施方式如下
typedef enum AnnotationType {
AnnotationTypeStart,
AnnotationTypeEnd,
AnnotationTypeWayPoint,
} AnnotationType;
@interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
AnnotationType ann;
}