2016-05-10 4 views
0

У меня есть собственный класс, где я могу добавить переменную типа, которая отличает этот тип, это возможность изменить значок, но я понятия не имею, где я могу сделать этот значок изменения, Я пытался сделать в методе MAPview, не получая импорт UIKit результата импорта CoreData импорт MapKitПользовательские аннотации к изображениям изображений

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 
     } 
    } 

я хочу получить direferent изображения в зависимости для 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 
    } 
+0

Черт, ваши гениальные! –

+0

Accpe ответ и голосование :) –

+1

echo.! (OK) excelente –

Смежные вопросы