- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的 MKPointAnnotation 应该使用此代码自定义:
-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{
Pin = [[MKPointAnnotation alloc] init];
Pin.title = title;
[Pin setCoordinate:Location];
[self mapView:mapView viewForAnnotation:Pin].annotation = Pin;
return Pin;
}
-(MKAnnotationView *)mapView: (MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>) annotation{
MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if(!pinView){
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.enabled = YES;
UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\
pinView.rightCalloutAccessoryView = PicButton;
}
else{
pinView.annotation = annotation;
}
return pinView;
}
但是,由于某种原因,Pin 仍然是默认的,有人可以帮我解决这个问题吗?谢谢
最佳答案
您不应该自己调用 viewForAnnotation
。您应该只向 map View 添加注释,然后它会确定在任何给定时刻哪些是可见的,并为您调用 viewForAnnotation
。因此:
考虑到不同的注释可以有不同的图像,你真的想继承 MKPointAnnotation
并给它一个 imageName
的属性(注意,不是 UIImage
本身,而是 viewForAnnotation
将能够用来创建 UIImage
本身的名称或标识符):
@interface MyAnnotation : MKPointAnnotation
@property(nonatomic, strong) NSString *imageName;
@end
@implementation MyAnnotation
@end
向您的 map View 添加注释(不是注释 View ),例如:
- (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName
{
MyAnnotation *annotation = [[MyAnnotation alloc] init];
annotation.title = title;
annotation.coordinate = coordinate;
annotation.imageName = imageName;
[mapView addAnnotation:annotation];
return annotation;
}
请注意,我不建议将 Pin
设为类 ivar/property,我会使用驼峰命名法(以小写字母开头),我会将方法名称从 setAnnotation
为 addAnnotationWithTitle
等内容
确保您的 Controller 已被指定为 mapView 的委托(delegate);
将为您调用viewForAnnotation
(如果注释可见)。它可能应该是这样的:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *customAnnotation = annotation;
static NSString *annotationIdentifier = @"MyAnnotation";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!annotationView)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
annotationView.annotation = annotation;
}
annotationView.image = [UIImage imageNamed:customAnnotation.imageName];
return annotationView;
}
return nil;
}
请注意,animatesDrop
是一个 MKPinAnnotationView
选项,您无法使用自定义注释 View 。但如果您愿意,很容易实现它(请参阅 How can I create a custom "pin-drop" animation using MKAnnotationView? )。我建议您先解决基本的注释 View ,然后再看看自定义注释 View 放置动画。
关于ios - 为什么我的 MKPointAnnotation 不是自定义的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507171/
我想用自定义 Logo 替换默认的 MKPointAnnotation Logo 。 所以我在 MapViewController 中编写了这段代码: import UIKit import MapK
我正在尝试向我的 MKPointAnnotations 添加一个按钮,然后对新的 View Controller 执行操作 我仔细研究了这两个问题 How to add button to MKPoi
简单问题:我似乎找不到 MKPointAnnotation 类的标记属性。它返回错误; MKPointAnnotation *annotation = [[HCIAnnotationViewContr
所以我有以下代码块,它应该迭代 JSON 对象数组并将 MKPointAnnotations 放在 map 上: for(id jsonObject in dataArray) { NSLog
我有 map View ,其中几乎没有 MKPointAnnotation。 一切正常,但是, View 的 MKPoiintAnnotation 的“背景”是“不可见的”,因此不是很“可见”。 我想
我是 Objective-C 的大三学生。我有一个任务将手势识别器添加到 MKPointAnnotation。我听说可以通过 MKMapView 委托(delegate)来完成。但我还不明白。 因此,
我创建了很多图钉,当我按下图钉时,标题必须显示,而副标题必须隐藏,因为它是一段很长的文本并且出现在 UItextView 中。问题是我没有找到隐藏字幕的方法,所以在标题下,我有一段很长的文字,结尾是:
是否可以向 MKPointAnnotation 添加额外的属性?目前有 coordinate , title , 和 subtitle . 是否可以添加 url可以在 -(MKAnnotationVi
我需要更改 MKPointAnnotation 对象的颜色,但我编写的方法似乎只生成红色图钉。该方法工作正常,但是当我调用该函数并传递给定常量之一的参数时, map 上出现的所有图钉都是红色的(默认值
为了在 Storyboard iOS 项目中创建 map 注释,我使用了: CLLocationCoordinate2D annotationCoord3; annotatio
在我的 viewWillAppear 中有一个 View MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; [point set
有没有办法阻止启用 MKMapView 实例中的注释。换句话说,当用户点击 map 上的红色图钉时,有没有办法阻止它突出显示图钉。现在,触摸时引脚会变黑... 编辑:我正在使用以下代码返回 MKPin
我想知道如何更新我的 annotation我拖动它时的标题,因为所有 annotation属性是get仅。 我尝试过的代码: func mapView(_ mapView: MKMapView, an
摘要:使用应用程序中的解析链接(而非查询)注释 已完成:我想做的是让新创建的注释成为 PFObject。我尝试执行此操作的方法是获取基于 MKPointAnnotation 的坐标。然后将其转换为带有
我正在尝试在 MapView 上显示两个注释。 为什么第二个注释不显示在 MapView 中?我尝试将地标[0]更改为放置标记[1],但没有帮助。 我本来可以使用 for 子句,但为了测试我重复了代码
我想知道当我点击标题时如何转到另一个 View MKPointAnnotation func addPin(lat:Double, lng:Double) { let testPin
我是 Xcode 新手,我从未使用 swift 创建过 iphone 应用程序,所以我在这方面仍然遇到很多麻烦。 在我的应用程序中,我想打开 map 页面,并希望为我拥有的每个地址显示一个图钉。为此,
我正在尝试在 MKPointAnnotations 上添加不同的图像。这是我的代码,当只有一个注释时它工作正常。我面临在不同注释上添加不同图像的问题。 func showDevices(Devs: A
当前正在使用 map View 并向 map 添加图钉。我知道如何使用 addAnotation() 方法向 map 添加单个点。现在,我尝试以最简单的方式向 MapView 添加多个点。我已经获取了
我有一个 map 应用程序,允许用户将注释保存为可变数组的收藏夹。然后可以在用户选择时显示所有喜欢的注释。 添加到可变数组的注释属于 MKPointAnnotation 类。我可以正确地向可变数组添加
我是一名优秀的程序员,十分优秀!