【问题标题】:Custom MKAnnotation Pin executing specific action when clicked -- custom pin not showing up单击时执行特定操作的自定义 MKAnnotation 引脚 - 自定义引脚未显示
【发布时间】:2020-07-06 20:18:29
【问题描述】:

我想知道是否有人可以帮助我使用 Xcode iOS Swift 应用程序。我对编码很陌生,当用户从菜单中单击某个按钮时,我试图删除带有标题/副标题/图像的自定义图钉/注释,但是当它出现时我无法显示该图钉在按钮的 IBAction 函数中。但是,当我从 IBAction 中删除 func mapView 并将其放置在 UIViewController 类中时,pin drop 确实有效(当我在另一个坐标处测试它时)但问题是它在主屏幕加载时自动出现。当我在 IBAction 按钮中包含 mapView 函数时,什么也没有发生,自定义注释也不会出现。

它还会使当前位置的蓝色用户点由于某种原因而消失。我想知道是否有人可以帮助仅删除自定义引脚的初始自动加载,并且仅在单击正确按钮时才执行相关功能。现在我只关注 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 函数:

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

还有我的 customPin 类:

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

}
}

【问题讨论】:

    标签: ios swift button mkmapview mkannotation


    【解决方案1】:

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

    试试这个:

    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
    }
    

    【讨论】:

    • 非常感谢您的回复!引脚落在我想要的位置,但似乎当 mapView 函数位于 IBAction func tulipButton 之外时,它会显示正确的自定义引脚,但它会在按下实际按钮之前自动将引脚放置在该位置。当我在 IBAction 按钮中移动 mapView 函数时,除了自定义图像部分之外,该引脚的行为与我预期的一样。所以似乎有脱节。我已将 annotationView.image 设置为自定义图像,将 tulipPin.image 设置为正确的图像,但它仍然没有显示
    • 你的“mapView 功能”是什么?
    • 您可以在上面找到更清晰的格式: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 }
    • 这不是您需要在按钮内部具有的功能。请参阅上面我编辑的答案。
    • 天哪,它成功了!非常感谢你是个传奇!! ^__^ 对我来说有一件事是 annotationView 调用不起作用,因为 pin 仍然不可点击,所以我看不到它的“标题”和“副标题”,你会碰巧知道原因吗?如果不是不用担心,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多