2017-01-26 5 views
0

次のコードはスタックオーバーフローに関する同じ質問です。しかし、コードが迅速になったように見えます。2.コードの一部に画像jjを追加しましたが、1つのエラーメッセージが表示されます。エラーメッセージはvar anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId)です。 reuseIdが未解決の識別子という唯一のエラーメッセージです。それが修正された場合、コードはコンパイルされます。この行のマップキットの未解決の識別子

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 

答えて

0

reuseIDがこの場所で知られていない - だけで、それは誤りreuseId is a unresolved identifierである理由thatsのif-elseブロック内:

var anView = thisMAP.dequeueReusableAnnotationView(withIdentifier: reuseId) 

あなたはそれがある範囲内で使用することができます定義された。

この行の前にreuseIDの定義を置きます!

それはあなたのようにreuselIDを定義するだろうかif anAnnotation.custom_image {

+0

で定義されたあなたのreuseIdと同じでなければなりませんか? –

+0

が更新されました – muescha

関連する問題