2

以下のメソッドにマップ注釈アイコン/ピンを追加する条件ステートメントがあります。私が抱えている問題は、マップにすべて同じアイコンが表示されていることです。猫の 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;

}

ここに画像の説明を入力

4

4 に答える 4

1

最後に次の行を追加します。

annView.annotation = annotation;
于 2013-10-22T10:19:46.163 に答える
0

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)
    ])
}
}
于 2020-06-16T17:13:23.883 に答える