我需要你的帮助。我正在开发一个应用程序,它显示了我的用户位置附近的一些位置。它显示了地点的名称和距离。我遇到的问题是添加图像,以便用户可以看到位置描述和图片。我该怎么做? 我的代码如下......
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)
}
}
}
我很感激你的帮助!谢谢
答案 0 :(得分:0)
试试这个:
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
}