【问题标题】:iOS Swift Google Maps - mapView didTapMarker not working?iOS Swift Google Maps - mapView didTapMarker 不工作?
【发布时间】:2018-06-15 02:40:42
【问题描述】:

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

点击标记会显示标记名称,但不会打印。我已经尝试了一些在网上找到的私有函数的变体,但也许我在这里遗漏了一些更基本的东西......

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

}

【问题讨论】:

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


    【解决方案1】:

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

    现在是

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

    现在工作

    【讨论】:

    • Reinier, developers.google.com/maps/documentation/ios-sdk/reference/… 谢谢 - 非常感谢。你能证实我的理解吗?如果乞求时显示(void),则该函数不指定其返回数据。 (BOOL) 表示该函数将返回一个布尔值,所以我必须声明它?我有点不确定 _ 的用途,但我会在那里做一些研究:)
    • Also.... 添加 bool 后,xcode 告诉我新的函数名称,但是上面的页面上没有列出该函数名称!它仍然是 didTapMarker :(
    • 您可以随时访问GMSMapViewDelegate 协议声明,看看它是如何在@ScottRobinson 那里声明的,但无论如何,swift 的自动完成功能必须对您有所帮助,在目标C 中,声明是- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker,这非常令人困惑
    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2015-06-05
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多