【问题标题】:Swift 4/Google Maps Custom Info Window Marker - can't get the "didTapAt"Swift 4/Google Maps 自定义信息窗口标记 - 无法获得“didTapAt”
【发布时间】:2018-07-29 23:50:52
【问题描述】:

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

我的最终目标是在标记的信息窗口中包含交互式元素(UIButton)。该标记恰好是“用户标记”或用户位置处的标记。经过一些调试,我可以看到一些代码根本就不会执行,但不清楚原因。我在下面添加了cmets来调出代码

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
}

}

附:如果你碰巧知道一个即插即用的 Pod 或其他一些超级容易重复使用的包 LMK(最好是最新的 Swift 版本或最新的 Obj-C)

【问题讨论】:

  • 回购的过期链接

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


【解决方案1】:

你可以添加代理来监听回调

 self.mapView.delegate = self

【讨论】:

    【解决方案2】:

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

    self.customInfoWindow = CustomInfoWindow().loadView()
    self.mapView.delegate = self
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 2014-11-13
      • 1970-01-01
      • 2021-12-15
      • 2014-02-18
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多