Iphone 设置MKAnnotation的图像
我使用以下代码在mapview中添加了MKAnnotationIphone 设置MKAnnotation的图像,iphone,objective-c,mkmapview,mkannotation,Iphone,Objective C,Mkmapview,Mkannotation,我使用以下代码在mapview中添加了MKAnnotation Flag *flag = [[Flag alloc] init]; flag.title = @"Golf Course"; flag.subtitle = @"Green"; flag.coordinate =CLLocationCoordinate2DMake(28.457394,77.108667); [mapView addAnnotation:flag]; 我把图像设置成这样 -(MKAnnotationView
Flag *flag = [[Flag alloc] init];
flag.title = @"Golf Course";
flag.subtitle = @"Green";
flag.coordinate =CLLocationCoordinate2DMake(28.457394,77.108667);
[mapView addAnnotation:flag];
我把图像设置成这样
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation
{
if(([annotation isKindOfClass:[Flag class]]))
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"PinId1";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"pin1"] autorelease];
//pinView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin1"];
pinView.canShowCallout = YES;
pinView.draggable = YES;
[pinView setImage:[UIImage imageNamed:@"flag2.png"]];
}
else
{
[mapView.userLocation setTitle:@"I am here"];
}
}
return pinView;
}
-(MKAnnotationView*)地图视图:(MKMapView*)mV视图用于注释:
(id)注释
{
if(([annotation isKindOfClass:[Flag class]]))
{
MKPinAnnotationView*pinView=nil;
if(注释!=mapView.userLocation)
{
静态NSString*defaultPinID=@“PinId1”;
pinView=(MKPinAnnotationView*)[mapView出列重用AnnotationViewWithIdentifier:defaultPinID];
如果(pinView==nil)pinView=[[[MKPinAnnotationView alloc]
initWithAnnotation:注释重用标识符:@“pin1”]自动释放];
//pinView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“pin1”];
pinView.canShowCallout=是;
pinView.draggable=是;
[pinView setImage:[UIImage ImageName:@“flag2.png”];
}
其他的
{
[mapView.userLocation setTitle:@“我在这里”];
}
}
返回pinView;
}
但是我没有得到MKAnnoation的图像。我怎么了?请帮助我。我也有同样的问题。现在使用下面给出的工作版本
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[Annotation class]])
{
static NSString* SFAnnotationIdentifier = @"SFAnnotationIdentifier";
MKPinAnnotationView* pinView =
(MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
if (!pinView)
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier] autorelease];
annotationView.canShowCallout = YES;
UIImage *flagImage = [UIImage imageNamed:@"map_pin.png"];
CGRect resizeRect;
resizeRect.size = flagImage.size;
CGSize maxSize = CGRectInset(self.view.bounds,
10.5,
10.5).size;
maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0;
if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationView.image = resizedImage;
annotationView.opaque = NO;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
Annotation *annoterObject = (Annotation *) annotation;
rightButton.tag = annoterObject.annoteCount;
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
-(MKAnnotationView*)映射视图:(MKMapView*)注释的映射视图:(id)注释
{
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
if([annotation isKindOfClass:[annotation class]])
{
静态NSString*SFAnnotationIdentifier=@“SFAnnotationIdentifier”;
MKPinAnnotationView*pinView=
(MKPinAnnotationView*)[self.mapView出列可重用AnnotationViewWithIdentifier:SFAnnotationIdentifier];
如果(!pinView)
{
MKAnnotationView*annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier]自动释放];
annotationView.canShowCallout=是;
UIImage*flagImage=[UIImage ImageName:@“map_pin.png”];
CGRect resizeRect;
resizeRect.size=flagImage.size;
CGSize maxSize=CGRectInset(self.view.bounds,
10.5,
10.5)尺寸;
maxSize.height-=self.navigationController.navigationBar.frame.size.height+40.0;
如果(resizeRect.size.width>maxSize.width)
resizeRect.size=CGSizeMake(maxSize.width,resizeRect.size.height/resizeRect.size.width*maxSize.width);
如果(resizeRect.size.height>maxSize.height)
resizeRect.size=CGSizeMake(resizeRect.size.width/resizeRect.size.height*maxSize.height,maxSize.height);
resizeRect.origin=(CGPoint){0.0f,0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage*resizedImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
annotationView.image=尺寸尺寸尺寸图像;
annotationView.不透明=否;
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
注释*注释对象=(注释*)注释;
rightButton.tag=annoterObject.annoteCount;
[rightButton添加目标:自我
操作:@选择器(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=rightButton;
返回注释视图;
}
其他的
{
pinView.annotation=注释;
}
返回pinView;
}
返回零;
}
这是工作代码。您必须在哪里设置图像?是否在标注上?如果是,则替换
[pinView setImage:[UIImage ImageName:@“flag2.png”]
withpinView.leftCalloutAccessoryView=[[UIImageView alloc]initWithImage:[UIImageName:@“flag2.png”]代码>遵循此链接问题很简单,就是您正在创建一个MKPinAnnotationView而不是MKAnnotationView。你不应该在答案中做所有的drawInRect的东西。是的。那是我犯的错误。谢谢。