ios 如何使用 iPhone 中的图像和标签在 Mapview 中添加注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18457975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add annotation in Mapview with image and label in IPhone?
提问by 2014
I am using MKMapview in my iPhone app.
我在我的 iPhone 应用程序中使用 MKMapview。
How can I assign values in annotation in MKMapkit Framework?
如何在 MKMapkit 框架的注释中分配值?
回答by iEinstein
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.
您可以使用自定义视图为每个注释使用一个UIView
、一个UIImageView
和一个 Label 来显示不同的值。这将在方法内完成- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation
。您必须UIView
采用透明颜色的两倍大小UIImageView
才能在放大和缩小地图视图时获得更精确的视图。
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.
希望这可以帮助。
回答by n00bProgrammer
- Create a custom Annotation class (
MKAnnotation
) - Create a custom AnnotationView class (
MKAnnotationView
)
- 创建自定义注解类 (
MKAnnotation
) - 创建自定义 AnnotationView 类 (
MKAnnotationView
)
Make sure you define the title, background image, and other elements of your custom annotation in the AnnotationView
class as properties.
确保在AnnotationView
类中将自定义注释的标题、背景图像和其他元素定义为属性。
Override the following method, and set the values of the properties you defined in
AnnotationView
class.- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ if ([<Object of CustomAnnotation class> isKindOfClass:[MKUserLocation class]]) return nil; CustomAnnotation* custom_anno = (CustomAnnotation*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"custom_annotation"]; if (!custom_anno){ custom_anno = [[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:@"custom_annotation"]; custom_anno.frame = CGRectMake(0, 0, 250, 57.5); custom_anno.canShowCallout = NO;//CHange if you want to change callout behaviour (thats is it's abilty to apper). I set it top no because i did not want a callout. UIImageView* icon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)]; [icon setImageWithURL:<your image> placeholderImage:<your placeholder image>];//placeholder image = nil if you do not want one. icon.backgroundColor = [UIColor lightGrayColor]; UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(50, 5, 200, 20)]; name.text = nameValue; UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 200, 20)]; category.text = otherValue; [custom_anno addSubview:icon]; [custom_anno addSubview:name]; [custom_anno addSubview:category]; } return custom_anno; }
My custom annotation class was 'Annotation' and custom annotation view class was 'CustomAnnotation'. Change according to your needs.
覆盖以下方法,并设置您在
AnnotationView
类中定义的属性的值。- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ if ([<Object of CustomAnnotation class> isKindOfClass:[MKUserLocation class]]) return nil; CustomAnnotation* custom_anno = (CustomAnnotation*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"custom_annotation"]; if (!custom_anno){ custom_anno = [[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:@"custom_annotation"]; custom_anno.frame = CGRectMake(0, 0, 250, 57.5); custom_anno.canShowCallout = NO;//CHange if you want to change callout behaviour (thats is it's abilty to apper). I set it top no because i did not want a callout. UIImageView* icon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)]; [icon setImageWithURL:<your image> placeholderImage:<your placeholder image>];//placeholder image = nil if you do not want one. icon.backgroundColor = [UIColor lightGrayColor]; UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(50, 5, 200, 20)]; name.text = nameValue; UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 200, 20)]; category.text = otherValue; [custom_anno addSubview:icon]; [custom_anno addSubview:name]; [custom_anno addSubview:category]; } return custom_anno; }
我的自定义注释类是“Annotation”,自定义注释视图类是“CustomAnnotation”。根据您的需要更改。
After this, just create an object of the Annotation
class, and use it.
在此之后,只需创建Annotation
该类的一个对象,并使用它。
EDIT :
编辑 :
Make sure you override the follwing method int he custom annotation view class:
确保在自定义注释视图类中覆盖以下方法:
in .h:
在.h:
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
in .m:
在.m:
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
}
EDIT 2 :
编辑 2:
You may use it in your view controller like this :
您可以像这样在视图控制器中使用它:
MKMapView* cellMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
cellMap.delegate = self;
cellMap.userInteractionEnabled = NO;
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(<your coordinates>);
MKCoordinateSpan span = MKCoordinateSpanMake(0.04, 0.04);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
cellMap.showsUserLocation = NO;
[cellMap setRegion:region animated:YES];
Annotation* anon = [[Annotation alloc] init];
anon.coordinate = center;
[cellMap addAnnotation:anon];
[checkIn addSubview:cellMap];