单击时自定义 MKAnnotation Pin 执行特定操作 - 自定义 pin 未显示

Custom MKAnnotation Pin executing specific action when clicked -- custom pin not showing up

我想知道是否有人可以帮助我使用我的 Xcode iOS Swift 应用程序。我对编码很陌生,当用户从菜单中单击某个按钮但我无法显示图钉时,我试图用 title/subtitle/image 删除自定义 pin/annotation当它在按钮的 IBAction 函数中时。但是,当我从 IBAction 中删除 func mapView 并将其放在 UIViewController class 中时,引脚掉落确实有效(当我在另一个坐标上测试它时)但问题是它在主屏幕加载时自动出现。当我在 IBAction 按钮中包含 mapView 函数时,没有任何反应,自定义注释也没有出现。

它也使当前位置蓝色用户点由于某种原因消失。我想知道是否有人可以帮助删除自定义 pin 的初始自动加载,并且仅在单击正确的按钮时才执行相关功能。现在我只关注 UI,所以坐标指向 SF 中的恶魔岛。非常感谢!!

我在下面附上了相关代码。

查看控制器代码:

 var locationManager = CLLocationManager()
 var tulipPin:customPin!
 @IBOutlet weak var mapView: MKMapView!


  @IBAction func tulipButton(_ sender: Any) {
    tulipPin = customPin(Title: "username", Subtitle: "caption", coordinate:CLLocationCoordinate2D(latitude: 37.8270, longitude: -122.4230))
    tulipPin.image = UIImage(named: "tulip")
                  self.mapView.addAnnotation(tulipPin)
    }

func mapView(_ mapview: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
             let annotationView = MKAnnotationView(annotation: tulipPin, reuseIdentifier: "tulip")
             annotationView.image = UIImage(named: "tulip")
           annotationView.canShowCallout = true
            annotationView.calloutOffset = CGPoint(x: -5, y: 5)
             let transform = CGAffineTransform(scaleX: 0.07, y: 0.07)
             annotationView.transform = transform
             return annotationView
    }

加上其他可能有助于破译的相关位置代码:

func determineCurrentLocation()
 {
     mapView.delegate = self
     locationManager = CLLocationManager()
     locationManager.delegate = self
     locationManager.desiredAccuracy = kCLLocationAccuracyBest
     locationManager.requestAlwaysAuthorization()
      if CLLocationManager.locationServicesEnabled() {
               locationManager.startUpdatingLocation()
           }
 }
 
 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
           mapView.delegate = self
           let center = CLLocationCoordinate2D(latitude: 37.8270, longitude: -122.4230)
           let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
            mapView.setRegion(region, animated: true)

这是主页上的 viewDidLoad 函数 viewcontroller:

 override func viewDidLoad() {
           
     super.viewDidLoad()
     regionSet()
     determineCurrentLocation()
 }

还有我的 customPin class:

import Foundation
import MapKit

class customPin: NSObject, MKAnnotation {
@IBOutlet weak var mapView: MKMapView!
  
   var coordinate: CLLocationCoordinate2D
   var title: String?
   var subtitle: String?
   var image: UIImage? = nil
   
init(Title: String, Subtitle: String, coordinate:CLLocationCoordinate2D) {
       self.title = Title
       self.subtitle = Subtitle
       self.coordinate = coordinate
        //self.image
    super.init()

}
}

有趣的是,我测试了您的代码,将坐标更改为纽约(模拟器位置)并且该按钮对我来说工作正常。您确定您的自定义坐标在您想要的位置吗?为什么不使用用户的位置?

试试这个:

func determineCurrentLocation() {
    mapView.delegate = self
    locationManager = CLLocationManager()
    locationManager.delegate = self
    mapView.showsUserLocation = true
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }
}

@IBAction func tulipButton(_ sender: Any) {
    dropPin()
}

func dropPin() { // Using the user's location
    guard let location = locationManager.location else { return }
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    tulipPin = customPin(Title: "username", Subtitle: "caption", coordinate: center)
    self.mapView.addAnnotation(tulipPin)
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locationManager.location else { return }
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
    mapView.setRegion(region, animated: true)
}

func mapView(_ mapview: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard !(annotation is MKUserLocation) else { return nil }
    let annotationView = MKAnnotationView(annotation: tulipPin, reuseIdentifier: "tulip")
    annotationView.image = UIImage(named: "tulip")
    annotationView.canShowCallout = true
    annotationView.calloutOffset = CGPoint(x: -5, y: 5)
//        let transform = CGAffineTransform(scaleX: 0.07, y: 0.07)
//        annotationView.transform = transform
    return annotationView
}