2017-05-25 5 views
0

Ich brauche deine Hilfe. Ich arbeite an einer App, die einige Orte in meiner Nähe zeigt. Es zeigt den Namen des Ortes und die Entfernung. Womit ich Probleme habe, ist ein Bild hinzuzufügen, so dass der Benutzer die Standortbeschreibung und ein Bild sehen kann. Wie kann ich es tun? Mein Code ist die folgende ...(Swift) Fügt meiner Annotation ein Callout hinzu

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

    let locationManager = CLLocationManager() 

    struct Location { 
    let title: String 
    let latitude: Double 
    let longitude: Double 
    } 


    let locations = [ 
    Location(title: "Saint Paul Hospital",  latitude: 49.280524700, longitude: -123.128232600) 
    ] 

    override func viewDidLoad() { 
    super.viewDidLoad() 

     mapita.showsUserLocation = true 

     for location in locations { 

     let annotation = MKPointAnnotation() 
     annotation.title = location.title 
     annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude) 


     if let currentLocation = locationManager.location?.coordinate { 
      let locationMapPoint = MKMapPointForCoordinate(currentLocation) 
      let pinMapPoint = MKMapPointForCoordinate(annotation.coordinate) 


      let distance = MKMetersBetweenMapPoints(locationMapPoint, pinMapPoint) 
      if distance >= 0 && distance <= 4500000 { 
       let distancia: Double = round (distance/1000) 
       annotation.subtitle = "Dist. \(distancia) kilometros" 
       mapita.addAnnotation(annotation) 
      } 
     } 
    } 

Dies ist, was ich bekomme. Was ich brauche, ist das Bild in die Callouts enter image description here

setzen Ich würde mich über Ihre Hilfe Jungs freuen! Danke

+0

ein UIImageView verwenden und es ist Bild – user1046037

Antwort

0

Versuchen Sie folgendes:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") 

    if annotationView == nil { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin") 
     annotationView!.canShowCallout = true 
     annotationView!.image = UIImage(named: "sun") 
    } else { 
     annotationView!.annotation = annotation 
    } 

    return annotationView 
} 
Verwandte Themen