2017-01-26 2 views
0

Der folgende Code war die gleiche Frage auf Stack-Überlauf. Es sieht jedoch so aus, als wäre der Code in swift 2. Ich habe ein Bild jj für einen Teil des Codes hinzugefügt, aber ich bekomme 1 Fehlermeldung. Die Fehlermeldung lautet var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId). Die Fehlermeldung besagt, dass reuseId ein nicht aufgelöster Bezeichner ist Das ist die einzige Fehlermeldung. Wenn das behoben ist, wird der Code kompiliert.unaufgelöster Bezeichner in Mapkit

import UIKit 
import MapKit 

class Annotation: NSObject, MKAnnotation 
{ 
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0) 
var custom_image: Bool = true 
var color: MKPinAnnotationColor = MKPinAnnotationColor.purple 
} 
class ViewController: UIViewController, MKMapViewDelegate { 
@IBOutlet var thisMAP: MKMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.thisMAP.delegate = self; 

    let annotation = Annotation() 
    thisMAP.addAnnotation(annotation) 

    let annotation2 = Annotation() 
    annotation2.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 1.0) 
    annotation2.custom_image = false 
    thisMAP.addAnnotation(annotation2) 

    let annotation3 = Annotation() 
    annotation3.coordinate = CLLocationCoordinate2D(latitude: 1.0, longitude: 0.0) 
    annotation3.custom_image = false 
    annotation3.color = MKPinAnnotationColor.green 
    thisMAP.addAnnotation(annotation3) 
} 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if (annotation is MKUserLocation) { 
     return nil 
    } 

    var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId) 

    if anView == nil { 
     if let anAnnotation = annotation as? Annotation { 
      if anAnnotation.custom_image { 
       let reuseId = "jj.png" 
       anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
       anView.image = UIImage(named:"jj.png") 
      } 
      else { 
       let reuseId = "pin" 
       let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
       pinView.pinColor = anAnnotation.color 
       anView = pinView 
      } 
     } 
     anView.canShowCallout = false 
    } 
    else { 
     anView.annotation = annotation 

Antwort

0

in dieser Zeile der reuseID an dieser Stelle nicht bekannt sind - nur innerhalb der if-else Blöcke sind, warum es der Fehler reuseId is a unresolved identifier:

var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId) 

Sie können es nur in dem Bereich verwenden, wo es ist definiert. Geben Sie die Definition für reuseID vor dieser Zeile ein!

sollte es das gleiche sein, wie Ihr reuseId Sie in der

+0

if anAnnotation.custom_image { definiert, was reuselID definieren würde? –

+0

hat die Frage aktualisiert – muescha