2017-12-19 5 views
0

wenn ich Googlemap bin mit alles, was ich tun muß, istWie füge ich ein CustomView Xib als Annotation in Ios hinzu?

let marker = GMSMarker() 
    marker.position = postions.target 
    marker.title = "TEST" 
    marker.map = self.mapView 
    let myCustomView:CustomView = UINib(nibName: "CustomView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomView 
    marker.iconView = myCustomView 

aber wenn ich MapKit verwende, wie soll ich dies erreichen.? Mycustomview ist eine XIB-Ansichtsdatei, die ich erstellt habe.

Antwort

2

versuchen, das Objekt der Spitze Blick auf die Anmerkungsansicht wie diese

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? 
{ 
    if annotation is MKUserLocation == true 
    { 

     return nil 
    } 


    let senderAnnotation = annotation as! MyAnnotation 

    let pinReusableIdentifier = senderAnnotation.pinColor.rawValue 

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinReusableIdentifier) 

    if annotationView == nil 
    { 

     annotationView = MKAnnotationView(annotation: senderAnnotation,                        reuseIdentifier: pinReusableIdentifier) 
      annotationView!.canShowCallout = true 



    } 

    let customView = (Bundle.main.loadNibNamed("directOrderView", owner: self, options: nil))?[0] as! directOrderView; 

    var calloutViewFrame = customView.frame; 
    calloutViewFrame.origin = CGPoint(x:-calloutViewFrame.size.width/2 + 30,y: -calloutViewFrame.size.height); 
    customView.frame = calloutViewFrame; 

    annotationView?.addSubview(customView) 

    return annotationView 

    } 
+0

ist das nicht callout Ansicht hinzufügen? Ich wollte eine benutzerdefinierte Ansicht der Pin nicht für Calloutview zuweisen .. meine PIN ist kein einfaches Bild, warum. –

+0

Nein, es ist nicht möglich, jede Ansicht, die Sie erstellen, zur Annotation hinzuzufügen. –

+0

Was ist MyAnnotation? Ich bin nicht klar hier "Annotation als! MyAnnotation" –

0

ok ich die Lösung gefunden

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     if annotation is MKUserLocation 
     { 
      return nil 
     } 
     var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin") 
     if annotationView == nil{ 
      annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin") 
      annotationView?.canShowCallout = false 
     }else{ 
      annotationView?.annotation = annotation 
      //annotationView?.canShowCallout = true 
      // mapView.deselectAnnotation(annotation, animated: true) 
      // mapView.selectAnnotation(annotation, animated: true) 
     } 
     let myCustomView:CustomView = UINib(nibName: "CustomView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomView 
     annotationView?.addSubview(myCustomView) 
    // annotationView?.image = UIImage(named: "starbucks") 

     return annotationView 
    } 
Verwandte Themen