如何在iPhone中使用图像和标签在Mapview中添加注释?(How to add annotation in Mapview with image and label in IPhone?)

编程入门 行业动态 更新时间:2024-10-20 08:48:15
如何在iPhone中使用图像和标签在Mapview中添加注释?(How to add annotation in Mapview with image and label in IPhone?)

我在我的iPhone应用程序中使用MKMapview。

我如何在MKMapkit Framework的注解中赋值?

I am using MKMapview in my iPhone app.

How can I assign values in annotation in MKMapkit Framework?

最满意答案

您可以使用自定义视图为每个注释使用一个UIView ,一个UIImageView和一个标签显示不同的值。 这将在方法内完成- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation 。 您必须将UIView的大小设置为透明颜色的两倍,以便更精确地查看放大和缩小mapview中的内容。

码 - - -

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString * const kPinAnnotationIdentifier = @"PinIdentifier"; if(annotation == self._mapView.userLocation) { return nil; } MyAnnotation *myAnnotation = (MyAnnotation *)annotation; MKAnnotationView *newAnnotation = (MKAnnotationView*)[self._mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier]; if(!newAnnotation){ newAnnotation = [[MKAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:@"userloc"]; } NSDictionary *dict=[alertInfoArray objectAtIndex:myAnnotation.ann_tag]; UIView *anView=[[UIView alloc] init]; anView.backgroundColor=[UIColor clearColor]; UIImageView *bgImg=[[UIImageView alloc] init]; bgImg.image=[UIImage imageNamed:@""]; bgImg.backgroundColor=[UIColor clearColor]; UIImageView *imgView=[[UIImageView alloc] init]; imgView.tag=myAnnotation.ann_tag; UILabel *lblName=[[UILabel alloc] init]; lblName.font=[UIFont systemFontOfSize:12]; lblName.textAlignment=UITextAlignmentCenter; lblName.textColor=[UIColor whiteColor]; lblName.backgroundColor=[UIColor clearColor]; lblName.text=TEXT YOU WANT ; newAnnotation.frame=CGRectMake(0, 0, 70, 212); anView.frame=CGRectMake(0, 0, 70, 212); bgImg.frame=CGRectMake(0, 0, 70, 106); bgImg.image=[UIImage imageNamed:@"thumb-needhelp.png"]; imgView.frame=CGRectMake(8,25,55,48); imgView.image=[UIImage imageNamed:@"girl-default.png"]; lblName.frame=CGRectMake(5,79,60,10); [anView addSubview:bgImg]; [bgImg release]; [anView addSubview:imgView]; [imgView release]; [newAnnotation addSubview:anView]; [anView release]; newAnnotation.canShowCallout=YES; [newAnnotation setEnabled:YES]; return newAnnotation; }

希望这可以帮助。

You can use custom view for each annotation with one UIView, one UIImageView and one Label to display different value. This will be done within the method - (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation. You have to take UIView double in size with transparent color with respect to UIImageView to make more precise view on zoom in and zoom out in mapview.

Code-----

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString * const kPinAnnotationIdentifier = @"PinIdentifier"; if(annotation == self._mapView.userLocation) { return nil; } MyAnnotation *myAnnotation = (MyAnnotation *)annotation; MKAnnotationView *newAnnotation = (MKAnnotationView*)[self._mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier]; if(!newAnnotation){ newAnnotation = [[MKAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:@"userloc"]; } NSDictionary *dict=[alertInfoArray objectAtIndex:myAnnotation.ann_tag]; UIView *anView=[[UIView alloc] init]; anView.backgroundColor=[UIColor clearColor]; UIImageView *bgImg=[[UIImageView alloc] init]; bgImg.image=[UIImage imageNamed:@""]; bgImg.backgroundColor=[UIColor clearColor]; UIImageView *imgView=[[UIImageView alloc] init]; imgView.tag=myAnnotation.ann_tag; UILabel *lblName=[[UILabel alloc] init]; lblName.font=[UIFont systemFontOfSize:12]; lblName.textAlignment=UITextAlignmentCenter; lblName.textColor=[UIColor whiteColor]; lblName.backgroundColor=[UIColor clearColor]; lblName.text=TEXT YOU WANT ; newAnnotation.frame=CGRectMake(0, 0, 70, 212); anView.frame=CGRectMake(0, 0, 70, 212); bgImg.frame=CGRectMake(0, 0, 70, 106); bgImg.image=[UIImage imageNamed:@"thumb-needhelp.png"]; imgView.frame=CGRectMake(8,25,55,48); imgView.image=[UIImage imageNamed:@"girl-default.png"]; lblName.frame=CGRectMake(5,79,60,10); [anView addSubview:bgImg]; [bgImg release]; [anView addSubview:imgView]; [imgView release]; [newAnnotation addSubview:anView]; [anView release]; newAnnotation.canShowCallout=YES; [newAnnotation setEnabled:YES]; return newAnnotation; }

Hope this helps.

更多推荐

本文发布于:2023-08-01 11:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   图像   标签   如何在   label

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!