2016-04-29 4 views
0

Hier ist mein Code. Ich habe alle verfügbaren Lösungen ausprobiert, aber ich bin nicht in der Lage, das Bild, das ich wünsche, anstelle des Standard-Anmerkungs-roten Stiftes in meinen Code zu stellen.Swift2 iOS9 - wie benutzerdefinierte Bild anstelle von Standard-Anmerkungs rote Pin in meinem Code erhalten

import MapKit 

class ViewController: UIViewController, MKMapViewDelegate { 

@IBOutlet var map: MKMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    var location = CLLocationCoordinate2DMake(18.956449, 72.810597) 

    var span = MKCoordinateSpanMake(0.009, 0.009) 
    var region = MKCoordinateRegion(center: location, span: span) 

    map.setRegion(region, animated: true) 

    var annotation = MKPointAnnotation() 
    annotation.coordinate = location 
    annotation.title = "Wilson College" 
    annotation.subtitle = "Commerce College" 

    map.addAnnotation(annotation) 
    print("showing on map") 

    mapView(map, viewForAnnotation: annotation) 


} 



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

    let identifier = "MyPin" 
    print("inside viewForAnnotation") 
    if annotation.isKindOfClass(MKUserLocation) { 
     return nil 
    } 

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure) 

    // Reuse the annotation if possible 
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

    if annotationView == nil 
    { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin") 
     annotationView!.canShowCallout = true 
     annotationView!.image = UIImage(named: "VRtest.png") 
     annotationView!.rightCalloutAccessoryView = detailButton 
     print("editing annotation") 
    } 
    else 
    { 
     annotationView!.annotation = annotation 
    } 

    return annotationView 
} 

enter image description here

Jede Hilfe ist willkommen. danke

+0

** NIE ** Anruf 'viewForAnnotation' alleine. Die Delegate-Methode wird vom Framework aufgerufen. – vadian

+0

PLZ Führer, ich bin ein Amateur. Ich habe einige Druckanweisungen angegeben, um den Codefluss zu verstehen. mit dem aktuellen Code, bekomme ich diese Anweisungen in der Ausgabebox, "auf der Karte anzeigen", "in ViewForAnnotation", "Bearbeitung von Annotation", und wenn ich die Zeile entfernen - mapView (map, viewForAnnotation: Annotation), dann bekomme ich das Aussage - nur auf der Karte anzeigen. – SagarWadekar

Antwort

0

Was möchten Sie die Standardannotation anpassen? Wenn ja, können Sie diesen Beitrag verweisen How to create Custom MKAnnotationView and custom annotation title and subtitle

Hier ist das Beispiel in Swift 2

class MapViewController: UIViewController { 


    @IBOutlet weak var mapView: MKMapView! 

    let initialLocation = CLLocation(latitude: 34.074094, longitude: -118.396329) 
    let regionRadius: CLLocationDistance = 1000 

    func centerMapOnLocation(location: CLLocation) { 
     let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 
      regionRadius * 2.0, regionRadius * 2.0) 
     self.mapView.setRegion(coordinateRegion, animated: true) 
    } 

    override func viewDidLoad() { 
     self.mapView.delegate = self 
     self.centerMapOnLocation(initialLocation) 

     let location = CLLocationCoordinate2D(latitude: 34.074094, longitude: -118.396329) 
     let annotation = CustomAnnotation(coordinate: location, imageName: "pinIcon", title: "Test", subtitle: "Detail", enableInfoButton: false) 

     self.mapView.addAnnotation(annotation); 
    } 
} 

extension MapViewController: MKMapViewDelegate { 
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

     if (annotation is MKUserLocation) { 
      return nil 
     } 

     let pinIdentifier = "pinIdentifier" 
     var view: MKAnnotationView 
     if (annotation.isKindOfClass(CustomAnnotation)) { 
      if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinIdentifier) 
       as MKAnnotationView? { 
        dequeuedView.annotation = annotation 
        view = dequeuedView 
      } else { 
       view = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdentifier) 
       view.canShowCallout = true 

       //Set custome image 
       let customAnnotation = annotation as! CustomAnnotation 
       view.image = UIImage(named:customAnnotation.imageName!) 
      } 

      return view 
     } 

     return nil 
    } 

} 

class CustomAnnotation : NSObject, MKAnnotation { 

    var coordinate: CLLocationCoordinate2D 
    var imageName: String? 
    var title: String? 
    var subtitle: String? 
    var enableInfoButton : Bool 

    init(coordinate: CLLocationCoordinate2D, imageName: String, title: String, subtitle: String, enableInfoButton : Bool) { 
     self.coordinate = coordinate 
     self.imageName = imageName 
     self.title = title 
     self.subtitle = subtitle 
     self.enableInfoButton = enableInfoButton; 
    } 
} 

Dies ist mein eigenes Bild auf dem Bildschirm

enter image description here

+0

dankbar für Ihre Hilfe. Die Antwort gibt Code für objectiveC, gibt es einen Code für swift2. Bitte teilen Sie den Link für Swift2-Code wenn möglich. Wenn es ein funktionierendes Beispielprojekt gibt, wäre das eine große Hilfe. – SagarWadekar

+0

Es hat funktioniert. HDT, du bist erstaunlich. danke für die Hilfe. – SagarWadekar

+0

Nur noch eine Sache, können wir Drop-Effekt für die Annotation haben? Was bearbeiten wir in diesem Code? – SagarWadekar

Verwandte Themen