iOS Swift谷歌地图 - mapView didTapMarker不工作?

时间:2018-01-05 09:49:53

标签: ios google-maps google-maps-api-3

所以我正在学习并了解swift / google maps API。但是,无论我尝试什么,当我点击标记时,我都无法在控制台中显示print语句。

点击标记会显示标记名称,但不会打印。我尝试过在网上找到的私人功能的一些变体,但也许我在这里错过了一些更基本的功能......

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")
    }

}

1 个答案:

答案 0 :(得分:4)

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

现在是

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
     print("Marker tapped")
}

立即工作