2016-05-10 6 views
0

このタイプを識別する変数「タイプ」を追加できるカスタムクラスがありますが、これはアイコンを変更するオプションですが、その変更アイコンをどこに作成できるかはわかりませんが、私は注釈ビューの画像をカスタマイズする

class MKAnnotationCostum: NSObject, MKAnnotation{ 

     var coordinate : CLLocationCoordinate2D 
     var title : String? 
     var subtitle : String? 
     var tipo:String? 
     var image: UIImage? 

     init (coordinate : CLLocationCoordinate2D , title : String , subtitle : String, tipo:String) { 
      self.coordinate = coordinate 
      self.title = title 
      self.subtitle = subtitle 
      self.tipo = tipo 
     } 
    } 

iはTIPO

import UIKit 
import MapKit 
import CoreData 

class ThreeViewController: UIViewController,MKMapViewDelegate { 

    @IBOutlet weak var myMap: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
      //viewDidAppear(true) 
      myMap.delegate = self 
     } 
    override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 


     let request = NSFetchRequest(entityName: "PUNTO") 
     if let results = try? context.executeFetchRequest(request) { 
      for resultado in results { 

       let latitud:CLLocationDegrees = Double(resultado.valueForKey("puntoLatitud") as! NSNumber) 
       let longitud:CLLocationDegrees = Double(resultado.valueForKey("puntoLongitud") as! NSNumber) 

       let lateDelta:CLLocationDegrees = 0.01 
       let longDelta:CLLocationDegrees = 0.01 

       let span: MKCoordinateSpan = MKCoordinateSpanMake(lateDelta, longDelta) 
       let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitud,longitud) 

       let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span) 


       myMap.setRegion(region, animated: true) 

       let pointAnnotation = MKAnnotationCostum(coordinate: location, title: resultado.valueForKey("puntoDireccion") as! String, subtitle: "Latitud: \(resultado.valueForKey("puntoLatitud")!), Longitud: \(resultado.valueForKey("puntoLongitud")!) ", tipo:resultado.valueForKey("clasId") as! String) 

//    pointAnnotation.coordinate = location 
//    pointAnnotation.title = String(resultado.valueForKey("puntoDireccion")!) 
//    pointAnnotation.subtitle = String("Latitud: \(resultado.valueForKey("puntoLatitud")!), Longitud: \(resultado.valueForKey("puntoLongitud")!) ") 

       print("color: \(pointAnnotation.tipo)") 

        myMap.addAnnotation(pointAnnotation) 
        //mapView(myMap, viewForAnnotation: pointAnnotation) 
      } 
     } 
     myMap.showsUserLocation = true 
    } 

    func mapView(mapaView: MKMapView, viewForAnnotation anotacion: MKAnnotation) -> MKAnnotationView? { 

     if !(anotacion is MKAnnotationCostum) { 

      let reusarId = "anotacion" 

      var anotacionView = mapaView.dequeueReusableAnnotationViewWithIdentifier(reusarId) 
      if anotacionView == nil { 
       anotacionView = MKAnnotationView(annotation: anotacion, reuseIdentifier: reusarId) 

       anotacionView!.canShowCallout = true 
       anotacionView!.leftCalloutAccessoryView = UIImageView(image: UIImage(named: "Amarillo")) 
      } 
      else { 

       anotacionView!.annotation = anotacion 
      } 

      return anotacionView 
     } 

     let reusarId = "anotacion" 

     var anotacionView = mapaView.dequeueReusableAnnotationViewWithIdentifier(reusarId) 
     let costum = anotacion as! MKAnnotationCostum 
     if anotacionView == nil { 
      anotacionView = MKAnnotationView(annotation: costum, reuseIdentifier: reusarId) 
      //let costumView = anotacionView as! MKAnnotationCostum 
      anotacionView!.image = UIImage(named:"Amarillo") 
      anotacionView!.leftCalloutAccessoryView = UIImageView(image: UIImage(named: "marca")) 
      anotacionView!.canShowCallout = true 

     }else { 
      anotacionView!.annotation = anotacion 
     } 
     return anotacionView 
    } 
+0

UIImage(名前: "Amarillo")がセットされるように設定されていますか? –

+0

はい、すべて同じ画像です –

+0

"tipo"に応じて変更したい –

答えて

0
func mapView(mapaView: MKMapView, viewForAnnotation anotacion: MKAnnotation) -> MKAnnotationView? { 

     if !(anotacion is MKAnnotationCostum) { 

      let reusarId = "anotacion" 

      var anotacionView = mapaView.dequeueReusableAnnotationViewWithIdentifier(reusarId) 
      if anotacionView == nil { 
       anotacionView = MKAnnotationView(annotation: anotacion, reuseIdentifier: reusarId) 

       anotacionView!.canShowCallout = true 
       anotacionView!.leftCalloutAccessoryView = UIImageView(image: UIImage(named: "Amarillo")) 
      } 
      else { 

       anotacionView!.annotation = anotacion 
      } 

      return anotacionView 
     } 

     let reusarId = "anotacion" 

     var anotacionView = mapaView.dequeueReusableAnnotationViewWithIdentifier(reusarId) 
     let costum = anotacion as! MKAnnotationCostum 
     if anotacionView == nil { 
      anotacionView = MKAnnotationView(annotation: costum, reuseIdentifier: reusarId) 
      //let costumView = anotacionView as! MKAnnotationCostum 
     if costum.tipo == "temp" { 
     anotacionView!.image = UIImage(named:"someImage") 
     } else { 
      anotacionView!.image = UIImage(named:"Amarillo") 
     } 
      anotacionView!.leftCalloutAccessoryView = UIImageView(image: UIImage(named: "marca")) 
      anotacionView!.canShowCallout = true 

     }else { 
      anotacionView!.annotation = anotacion 
     } 
     return anotacionView 
    } 
012用応じた画像direferentを取得したい 輸入CoreData 輸入MapKit結果 輸入のUIKitを得ることなく、メソッドのMapViewで行うことを試みています
+0

くそー、あなたは気楽です! –

+0

Accpeの答えと上の票: –

+1

エコー! (OK)excelente –

関連する問題