Swift 4 /谷歌地图自定义信息窗口标记 - 无法获得“didTapAt”

时间:2018-02-19 17:33:25

标签: ios swift google-maps google-maps-markers

我搜索了互联网的高低。我看到了大量相同代码的示例,这里的代码主要是“复制/粘贴”。但我无法让它发挥作用。

我的最终目标是在标记的信息窗口中添加交互元素(UIButton)。该标记恰好是“用户标记”或用户位置点的标记。经过一些调试后,我可以看到一些代码根本就没有执行过,但目前还不清楚为什么。我在下面添加了评论来调出代码

My repo is here, feel free to poke at it

class MapViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet var mapView: GMSMapView!
var customInfoWindow:CustomInfoWindow?
var marker = GMSMarker()
var locationManager = CLLocationManager()
let zoom:Float = 15

override func viewDidLoad() {
    super.viewDidLoad()
    // ALL THIS WORKS FINE

    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()
    self.customInfoWindow = CustomInfoWindow().loadView()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    guard status == .authorizedWhenInUse else {
        return
    }
   // ALL THIS WORKS FINE

    locationManager.startUpdatingLocation()
    mapView.isMyLocationEnabled = true
    mapView.settings.myLocationButton = true
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.first else {
        return
    }
   // ALL THIS WORKS FINE

    mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: self.zoom, bearing: 0, viewingAngle: 0)
    marker.position = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    marker.isTappable = true
    //marker.title = "Hi"
    marker.map = self.mapView
    locationManager.stopUpdatingLocation()
}

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    self.marker = marker

    // NONE OF THIS CODE APPEARS TO EXECUTE

    //get position of tapped marker
    let position = marker.position
    mapView.animate(toLocation: position)
    let point = mapView.projection.point(for: position)
    let newPoint = mapView.projection.coordinate(for: point)
    let camera = GMSCameraUpdate.setTarget(newPoint)
    mapView.animate(with: camera)

    let opaqueWhite = UIColor(white: 1, alpha: 0.85)
    customInfoWindow?.layer.backgroundColor = opaqueWhite.cgColor
    customInfoWindow?.layer.cornerRadius = 8
    customInfoWindow?.center = mapView.projection.point(for: position)
    customInfoWindow?.center.y -= 100
    customInfoWindow?.customWindowLabel.text = "This is my Custom Info Window"
    customInfoWindow?.removeFromSuperview()
    self.mapView.addSubview(customInfoWindow!)

    return false
}

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
   // NONE OF THIS CODE APPEARS TO EXECUTE
    return UIView()
}

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
 // NONE OF THIS CODE APPEARS TO EXECUTE
    customInfoWindow?.removeFromSuperview()
}

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
// NONE OF THIS CODE APPEARS TO EXECUTE - and i don't expect it to unless there is a position change, but wanted to make a note

    let position = self.marker.position
    customInfoWindow?.center = mapView.projection.point(for: position)
    customInfoWindow?.center.y -= 100
}

}

P.S。如果您碰巧知道即插即用吊舱或其他一些超级易用的可重复使用的包装LMK(最新的Swift版本或最新的Obj-C将是最好的)

2 个答案:

答案 0 :(得分:0)

您可以添加委托来收听回调

 self.mapView.delegate = self

答案 1 :(得分:0)

关键是向ViewController添加两行。首先,我忘了实例化自定义窗口视图的实例,然后如上所述,我忘了为回调添加委托

createvaluetraversalbasis