当调用函数时,如何自动显示mkpointantation标题?

发布于 2025-01-21 14:40:13 字数 1760 浏览 3 评论 0原文

我正在创建一个函数,当它被调用时,它将放大自定义PIN注释坐标并显示其标题和字幕。变焦部分正常,我唯一做的是标题和字幕部分。

 private func addCustomPinRectoría1() {
        let pin1 = MKPointAnnotation()
        pin1.coordinate = coordinatesRectoría1
        pin1.title = "Rectoría"
        pin1.subtitle = "Parada TigreBus"
        map.addAnnotation(pin1)

  func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
        guard let coordinates = coordinates else {
            return
        }
        map.removeAnnotations(map.annotations)
        let selectedPin = MKPointAnnotation()
        selectedPin.coordinate = coordinates
        map.addAnnotation(selectedPin)

        // Here is where I need to display the pin's title and subtitle automatically

        map.setRegion(MKCoordinateRegion(
        center: coordinates,
        span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
                      animated: true)
        
    }   

我不知道这是否有用,但是我也将留下代码的这一部分:

  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }
        var annotationView = map.dequeueReusableAnnotationView(withIdentifier: "custom")
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "custom")
            annotationView?.canShowCallout = true
        }
        else {
            annotationView?.annotation = annotation
        }
        annotationView?.image = UIImage(named: "TigreBusParada")
        annotationView?.frame.size = CGSize(width: 35, height: 100)
        return annotationView
    }

I'm creating a function that when it's called, it will zoom in into the custom pin annotation coordinates and show its title and subtitle. The zoom part works fine, the only thing I can't do it's the title and subtitle part.

 private func addCustomPinRectoría1() {
        let pin1 = MKPointAnnotation()
        pin1.coordinate = coordinatesRectoría1
        pin1.title = "Rectoría"
        pin1.subtitle = "Parada TigreBus"
        map.addAnnotation(pin1)

  func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
        guard let coordinates = coordinates else {
            return
        }
        map.removeAnnotations(map.annotations)
        let selectedPin = MKPointAnnotation()
        selectedPin.coordinate = coordinates
        map.addAnnotation(selectedPin)

        // Here is where I need to display the pin's title and subtitle automatically

        map.setRegion(MKCoordinateRegion(
        center: coordinates,
        span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
                      animated: true)
        
    }   

I don't know if this is useful, but I'll also leave this part of the code:

  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }
        var annotationView = map.dequeueReusableAnnotationView(withIdentifier: "custom")
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "custom")
            annotationView?.canShowCallout = true
        }
        else {
            annotationView?.annotation = annotation
        }
        annotationView?.image = UIImage(named: "TigreBusParada")
        annotationView?.frame.size = CGSize(width: 35, height: 100)
        return annotationView
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2025-01-28 14:40:13

将其添加到MapView之后,选择注释。这是代码。

func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
    guard let coordinates = coordinates else {
        return
    }
    map.removeAnnotations(map.annotations)
    let selectedPin = MKPointAnnotation()
    selectedPin.coordinate = coordinates
    map.addAnnotation(selectedPin)
    
    //add this line to show the title and subtitle
    map.selectAnnotation(selectedPin, animated: false)
    
    map.setRegion(MKCoordinateRegion(
        center: coordinates,
        span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
                  animated: true)
    
}

Select annotation after adding it to mapView. Here is the code.

func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
    guard let coordinates = coordinates else {
        return
    }
    map.removeAnnotations(map.annotations)
    let selectedPin = MKPointAnnotation()
    selectedPin.coordinate = coordinates
    map.addAnnotation(selectedPin)
    
    //add this line to show the title and subtitle
    map.selectAnnotation(selectedPin, animated: false)
    
    map.setRegion(MKCoordinateRegion(
        center: coordinates,
        span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
                  animated: true)
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文