我有一个条件语句,在下面的方法中添加地图注释图标旋转。我遇到的问题是,地图被填充了所有相同的图标。它应该检测猫的id,并根据检测到的猫的id来显示图标。我不知道问题出在哪里,因为这在iOS 6中确实有效,而现在在iOS 7中,地图只显示所有相同的注解图标图像。
- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation {
annView = nil;
if(annotation != mapingView.userLocation)
{
static NSString *defaultPinID = @"";
annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( annView == nil )
annView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annView.rightCalloutAccessoryView = rightButton;
MyAnnotation* annotation= [MyAnnotation new];
annotation.catMapId = categoryIdNumber;
NSLog(@"categoryIdNumber %@",categoryIdNumber);
NSLog(@"annotation.catMapId %@",annotation.catMapId);
if (annotation.catMapId == [NSNumber numberWithInt:9]) {
annView.image = [UIImage imageNamed:@"PIN_comprare.png"];
NSLog(@"annview 9");
}
else if (annotation.catMapId == [NSNumber numberWithInt:10]) {
annView.image = [UIImage imageNamed:@"PIN_mangiare.png"];
NSLog(@"annview 10");
}
else if (annotation.catMapId == [NSNumber numberWithInt:11]) {
annView.image = [UIImage imageNamed:@"PIN_visitare.png"];
NSLog(@"annview 11");
}
else if (annotation.catMapId == [NSNumber numberWithInt:12]) {
annView.image = [UIImage imageNamed:@"PIN_vivere.png"];
NSLog(@"annview 12");
}
annView.canShowCallout = YES;
}
return annView;
}
如果像你说的那样,"这在iOS 6中确实有效",你应该考虑一下 颇为 幸运的是,它做到了(或似乎做到了),这种设置注释的图像的方法在任何版本下都不应该被依赖。
虽然@阿马说的没错,注解视图的 annotation
属性应该被设置(以防视图被重复使用),但这并不能解决主要问题。
注释视图的 image
的值为基础设置。categoryIdNumber
似乎是某种变数 外面 的 viewForAnnotation
委托方法。
您 不能 假设。
viewForAnnotation
将会在你调用之后立即被调用 addAnnotation
. 即使在iOS 6或更早的版本中,也不能保证这一点。viewForAnnotation
每个注解将只被调用一次。 当用户平移或缩放地图并将注解返回到屏幕上时,同一注解的委托方法可以被多次调用。viewForAnnotation
将以添加注解的相同顺序调用该方法。 这是第1和第2点的结果。我假设就在您调用 addAnnotation
జజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ categoryIdNumber
是正确设置的,然后根据上述不正确的假设。viewForAnnotation
用途 categoryIdNumber
来设置图像。
现在的情况是 viewForAnnotation
正在被地图视图调用,毕竟或部分的 addAnnotation
这时 categoryIdNumber
可能是与最后添加的注解相关的值,所有注解都使用适用于最后注解的图像。
为了解决这个问题(不管 的iOS版本),您必须将正确的 categoryIdNumber
值到每个注解对象中 之前 呼叫 addAnnotation
.
看起来你的注解类是 MyAnnotation
而你已经有了 catMapId
属性。
您必须在注解中设置这个属性 之前 呼叫 addAnnotation
-- 不是 里面 的 viewForAnnotation
方法,这就太晚了。 顺便说一下,你正在创建一个 MyAnnotation
对象 里面 的 viewForAnnotation
方法,这是毫无意义的。)
所以,在你创建和添加注释的地方(不是在 viewForAnnotation
):
MyAnnotation* myAnn = [[MyAnnotation alloc] init];
myAnn.coordinate = ...
myAnn.title = ...
myAnn.catMapId = categoryIdNumber; // <-- set catMapId BEFORE addAnnotation
[mapView addAnnotation:myAnn];
那就把代码放在 viewForAnnotation
应该是这样的。
- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation
{
annView = nil;
if(annotation != mapingView.userLocation)
{
static NSString *defaultPinID = @"MyAnnId";
annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( annView == nil )
{
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
annView.canShowCallout = YES;
}
else
{
//view is being re-used, re-set annotation to current...
annView.annotation = annotation;
}
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annView.rightCalloutAccessoryView = rightButton;
//Make sure we have a MyAnnotation-type annotation
if ([annotation isKindOfClass:[MyAnnotation class]])
{
//Do not CREATE a local MyAnnotation object here.
//Instead, get the catMapId from the annotation object
//that was PASSED INTO the delegate method.
//MyAnnotation* annotation= [MyAnnotation new];
//annotation.catMapId = categoryIdNumber;
MyAnnotation *myAnn = (MyAnnotation *)annotation;
//The value of the external variable categoryIdNumber is irrelevant here.
//NSLog(@"categoryIdNumber %@",categoryIdNumber);
NSLog(@"myAnn.catMapId %@",myAnn.catMapId);
//Put the NSNumber value into an int to simplify the code below.
int myAnnCatMapId = [myAnn.catMapId intValue];
NSString *imageName = nil;
switch (myAnnCatMapId)
{
case 9:
{
imageName = @"PIN_comprare.png";
break;
}
case 10:
{
imageName = @"PIN_mangiare.png";
break;
}
case 11:
{
imageName = @"PIN_mangiare.png";
break;
}
case 12:
{
imageName = @"PIN_vivere.png";
break;
}
default:
{
//set some default image for unknown cat ids...
imageName = @"default.png";
break;
}
}
annView.image = [UIImage imageNamed:imageName];
NSLog(@"annview %d", myAnnCatMapId);
}
}
return annView;
}
在最后加上这一行
annView.annotation = annotation;
同样的麻烦,对我来说,决定不使用...
pinView.animatesDrop = YES;
对我来说,自定义图标不能与动画投放一起使用。
如果有人需要使用MapView注释作为一个tableView,即有一个点的数组显示在地图上。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = MKMapViewDefaultAnnotationViewReuseIdentifier
if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? TrailAnnotationView {
///TrailAnnotation has an object of type trail ( can be any model you ///want ). and so is TrailAnnotationView
///inside TrailAnnotationView we extract data from trail and display it.
annotationView.trail = (annotation as? TrailAnnotation)?.trail
annotationView.annotation = annotation
return annotationView
}
let annotationView = TrailAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.trail = (annotation as? TrailAnnotation)?.trail
annotationView.canShowCallout = true
return annotationView
}
这是一个TrailAnnotationView
protocol AnnotationViewProtocol {
func didTapOnAnnotation()
}
class TrailAnnotationView: MKPinAnnotationView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
var trail: TrailModel? = nil
override var annotation: MKAnnotation? { didSet { configureDetailView() } }
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
}
private extension TrailAnnotationView {
func configure() {
canShowCallout = true
configureDetailView()
}
func configureDetailView() {
guard let annotation = annotation else { return }
let rect = CGRect(origin: .zero, size: CGSize(width: 300, height: 200))
let snapshotView = UIView()
snapshotView.translatesAutoresizingMaskIntoConstraints = false
if let trail = self.trail, !trail.imageUrl.isEmpty {
AppUtils.sharedInstance.fetchImageFor(path: trail.imageUrl) { (image) in
guard let imageData = image else { return }
DispatchQueue.main.async {
let imageView = UIImageView(frame: rect)
imageView.image = imageData
snapshotView.addSubview(imageView)
}
}
} else {
let options = MKMapSnapshotter.Options()
options.size = rect.size
options.mapType = .satelliteFlyover
options.camera = MKMapCamera(lookingAtCenter: annotation.coordinate, fromDistance: 250, pitch: 65, heading: 0)
let snapshotter = MKMapSnapshotter(options: options)
snapshotter.start { snapshot, error in
guard let snapshot = snapshot, error == nil else {
print(error ?? "Unknown error")
return
}
let imageView = UIImageView(frame: rect)
imageView.image = snapshot.image
snapshotView.addSubview(imageView)
}
}
detailCalloutAccessoryView = snapshotView
NSLayoutConstraint.activate([
snapshotView.widthAnchor.constraint(equalToConstant: rect.width),
snapshotView.heightAnchor.constraint(equalToConstant: rect.height)
])
}
}