Swift:如何制作引脚掉落位置的自定义标题视图?

时间:2017-10-18 14:38:37

标签: 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()

    }

}

2 个答案:

答案 0 :(得分:0)

尝试使用此方法来处理敲击

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

答案 1 :(得分:0)

如果您想在标注中显示该位置的名称,您应该-mapView:annotationCanShowCallout:返回true。

func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    return true
}

这允许注释显示标注。您还可以在该方法中访问该批注的属性(例如标题或副标题)。您可能会发现此示例很有用:https://www.mapbox.com/ios-sdk/examples/marker/