gpt4 book ai didi

swift - (Swift) 将图片添加到我的注释标注中

转载 作者:行者123 更新时间:2023-11-30 12:26:17 25 4
gpt4 key购买 nike

我需要你的帮助。我正在开发一个应用程序,它显示我的用户位置附近的一些位置。它显示了地点的名称和距离。我遇到的问题是添加图像,以便用户可以看到位置描述和图片。我该怎么做?我的代码如下...

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

这就是我得到的。我需要的是将图像放在标注内 enter image description here

非常感谢你们的帮助!谢谢

最佳答案

试试这个:

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
}

关于swift - (Swift) 将图片添加到我的注释标注中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44170856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com