ios - Swift:如何制作图钉放置位置的自定义标题 View ?

标签 ios swift annotations mapkit mapbox

所以我正在使用 Swift 4 并且希望有一个引脚掉落,然后当用户触摸该引脚时我也希望它能显示当前位置的文本...当我运行它时,引脚掉落得很好,但是之后我无法点击引脚,因此与该标记点完全没有交互。我应该注意,我正在使用 Mapbox。

import Foundation
import UIKit
import CoreLocation
import Mapbox
import MapKit

class SecondViewController: UIViewController, CLLocationManagerDelegate, MGLMapViewDelegate {

@IBOutlet var mapView: MGLMapView!
        let manager = CLLocationManager()

override func viewDidLoad() {
            super.viewDidLoad()
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.requestWhenInUseAuthorization()

        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()

}
        @IBAction func markStuff(_ sender: Any) {
}
        @IBAction func refLocation(_ sender: Any) {
                manager.startUpdatingLocation()

}

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location = locations[0]

            let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

            mapView.setCenter(center, zoomLevel: 10, animated: true)

            let annotation = MGLPointAnnotation()

            annotation.coordinate = location.coordinate

            annotation.title = "\(annotation.coordinate)"

            self.mapView.addAnnotation(annotation)

            manager.stopUpdatingLocation()

    }

}

最佳答案

尝试此方法来处理 pin 上的点击

func mapView(mapView: MGLMapView, tapOnCalloutForAnnotation annotation: MGLAnnotation) 
{

     // Do something here
       print("Do Something here You Want)
}

另请参阅 Mapbox 文档 https://www.mapbox.com/ios-sdk/api/3.0.0/Protocols/MGLMapViewDelegate.html#//api/name/mapView:tapOnCalloutForAnnotation

关于ios - Swift:如何制作图钉放置位置的自定义标题 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812801/

相关文章:

ios - for 循环 CGFloat 出错

spring - 过滤@ComponentScan 中的特定包

ios - 导航 Controller 弹出 subview 后,强制 parentView 刷新 View

android - 如何获取网络状态?

ios - UICollectionView 滚动整个页面

swift - 由于另一个进程, Realm 浏览器未打开

ios - 无法在 swift 3 的结构中获取返回值

java - hibernate/JPA : Map a legacy database Not Null Foreign Key 'Dummy' value as NULL

security - Symfony2.3 自定义投票者通过@SecureParam

ios - NSPredicate 比较数字问题