iOS Swift Google 地图 - mapView didTapMarker 不起作用?

Sco*_*son 4 google-maps google-maps-api-3 ios

所以我正在学习并了解 swift/google 地图 API。但是,无论我尝试什么,当我点击标记时,我都无法让打印语句出现在控制台中。

点击标记会显示标记名称,但它永远不会打印。我尝试了在网上找到的 private func 的一些变体,但也许我在这里遗漏了一些更基本的东西......

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        struct MarkerStruct {
            let name: String
            let lat: CLLocationDegrees
            let long: CLLocationDegrees
        }

        let markers = [
            MarkerStruct(name: "Food Hut 1", lat: 52.649030, long: 1.174155),
            MarkerStruct(name: "Foot Hut 2", lat: 52.649030, long: 1.174185),
        ]

        let camera = GMSCameraPosition.camera(withLatitude: 52.649030, longitude: 1.174155, zoom: 14)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        mapView.delegate = self
        self.view = mapView

        for marker in markers {
            let position = CLLocationCoordinate2D(latitude: marker.lat, longitude: marker.long)
            let locationmarker = GMSMarker(position: position)
            locationmarker.title = marker.name
            locationmarker.map = mapView
        }
    }

    private func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) {
        print("Marker tapped")
    }

}
Run Code Online (Sandbox Code Playgroud)

Rei*_*ian 5

您使用的是旧方法名称,并且还需要删除关键字private,这就是您的委托方法未被调用的原因

现在是

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
     print("Marker tapped")
     return true 
}
Run Code Online (Sandbox Code Playgroud)

现在工作