ユーザーがGoogleマップのマーカーをタップしたときに表示されるカスタムビューを作成しました。だから私のようにデリゲートメソッドmarkerInfoWindow
を書いてきました:カスタムマーカースニペットがボタンアクションターゲットを呼び出していません
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let infoWindow = Bundle.main.loadNibNamed("emergencyInfo", owner: self.view, options: nil)!.first! as! emergencyInfo
infoWindow.frame = CGRect(x: 0, y: 0, width: 200, height: 110)
infoWindow.titleOfCenter.text = marker.title
infoWindow.addressOfCenter.text = marker.snippet
infoWindow.callNowButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return infoWindow
}
とbuttonTapped
のfuctionは
@objc func buttonTapped(sender: UIButton) {
print("Yeah! Button is tapped!")
}
として実装されているが、問題はそれが関数内で行っていないということです。私はthis tutorialをいれました答えに基づいて
UPDATE
ので、これは私が実装したものです:didTap
に
var tappedMarker = GMSMarker()
var infoWindow = emergencyInfo(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
その後:
まず、私はこれらの2つの変数を宣言しました私はこれをしました: func mapView(_mapView:GMSMapView、didTapマーカー:GMSMarker) - > Bool {
let location = CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude)
tappedMarker = marker
infoWindow.removeFromSuperview()
infoWindow = emergencyInfo(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
infoWindow.center = mapView.projection.point(for: location)
infoWindow.callNowButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) //getting error on this line
self.view.addSubview(infoWindow)
return false
}
そして最後に、私はにmarkerInfoWindow
方法を変更します。今、私は、コードを実行したときに、私はいくつかに基づいて#selector(buttonTapped)
fatal error: unexpectedly found nil while unwrapping an Optional value
を設定しています行にエラーが発生します
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}
私がちょうど今"Title"
与えたラベルinfoWindow.titleOfCenter.text
を持っているので、私はこのエラーが左手側、すなわちinfoWindow.callNowButton
のために発生していることがわかりましたそれはクラッシュします同じエラーが表示されます。
Salam @ChaudhryTalha私のcustominfomarkerの中心点が間違った位置になりました。すなわち、マーカをタップすると、カスタムインフォーマラはマーカ上の中央に正しい位置を示さない。あなたはそれについてのアイデアがあれば分かち合ってください。 –