如何在没有 InfoView 的情况下在 GMSMarker 上添加点击事件
How to add tap event on GMSMarker without InfoView
我是 Swift 的初学者,是一名来自日本的学生。所以我的英语不好。但是请阅读我的代码并给我解决方案。
我想在点击 GMSMarker 时弹出详细视图
但是,当我点击 GMSMarker 时,将调用 infoView,然后点击 infoView,详细视图显示在此代码上。
我该如何修改这个?
GMSMarker 没有 infoView 是不是可以直接进入详细视图??
请帮帮我。
//MARK: textfield
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let autoCompleteController = GMSAutocompleteViewController()
autoCompleteController.delegate = self
let filter = GMSAutocompleteFilter()
autoCompleteController.autocompleteFilter = filter
self.locationManager.startUpdatingLocation()
self.present(autoCompleteController, animated: true, completion: nil)
return false
}
// MARK: GOOGLE AUTO COMPLETE DELEGATE
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let lat = place.coordinate.latitude
let long = place.coordinate.longitude
showPartyMarkers(lat: lat, long: long)
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)
myMapView.camera = camera
txtFieldSearch.text=place.formattedAddress
chosenPlace = restaurant(name: place.formattedAddress!, lat: lat, long: long)
let restaurantMarker=GMSMarker()
restaurantMarker.position = CLLocationCoordinate2D(latitude: lat, longitude: long)
restaurantMarker.title = "\(place.name)"
restaurantMarker.snippet = "\(place.formattedAddress!)"
restaurantMarker.map = myMapView
self.dismiss(animated: true, completion: nil) // dismiss after place selected
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("ERROR AUTO COMPLETE \(error)")
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
self.dismiss(animated: true, completion: nil)
}
func initGoogleMaps() {
let camera = GMSCameraPosition.camera(withLatitude: 28.7041, longitude: 77.1025, zoom: 17.0)
self.myMapView.camera = camera
self.myMapView.delegate = self
self.myMapView.isMyLocationEnabled = true
}
// MARK: CLLocation Manager Delegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while getting location \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.delegate = nil
locationManager.stopUpdatingLocation()
let location = locations.last
let lat = (location?.coordinate.latitude)!
let long = (location?.coordinate.longitude)!
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)
self.myMapView.animate(to: camera)
showPartyMarkers(lat: lat, long: long)
}
// MARK: GOOGLE MAP DELEGATE restaurantMarkerがタップされた時の動き
func mapView(_ mapView: GMSMapView, didTap restaurantMarker: GMSMarker) -> Bool {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return false }
let img = customMarkerView.img!
let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: img, borderColor: UIColor.white, tag: customMarkerView.tag)
restaurantMarker.iconView = customMarker
return false
}
func mapView(_ mapView: GMSMapView, markerInfoContents restaurantMarker: GMSMarker) -> UIView? {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return nil }
let data = previewDemoData[customMarkerView.tag]
restaurantPreviewView.setData(title: data.title, img: data.img, price: data.price)
return restaurantPreviewView
}
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf restaurantMarker: GMSMarker) {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return }
let tag = customMarkerView.tag
restaurantTapped(tag: tag)
}
func mapView(_ mapView: GMSMapView, didCloseInfoWindowOf restaurantMarker: GMSMarker) {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return }
let img = customMarkerView.img!
let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: img, borderColor: UIColor.darkGray, tag: customMarkerView.tag)
restaurantMarker.iconView = customMarker
}
//マーカーがどう動くか
func showPartyMarkers(lat: Double, long: Double) {
myMapView.clear()
for restaurant in restaurants {
let restaurantMarker=GMSMarker()
let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: previewDemoData[0].img, borderColor: UIColor.darkGray, tag: 0)
//[0]の部分をiにしてデータから順に引張てくるようにすれば良い
restaurantMarker.position = CLLocationCoordinate2D(latitude: restaurant.lat, longitude: restaurant.long)
restaurantMarker.iconView=customMarker
restaurantMarker.map = self.myMapView
}
}
@objc func btnMyLocationAction() {
let location: CLLocation? = myMapView.myLocation
if location != nil {
myMapView.animate(toLocation: (location?.coordinate)!)
}
}
@objc func restaurantTapped(tag: Int) {
let v=DetailsVC()
v.passedData = previewDemoData[tag]
self.navigationController?.pushViewController(v, animated: true)
}
func setupTextField(textField: UITextField, img: UIImage){
textField.leftViewMode = UITextFieldViewMode.always
let imageView = UIImageView(frame: CGRect(x: 5, y: 5, width: 20, height: 20))
imageView.image = img
let paddingView = UIView(frame:CGRect(x: 0, y: 0, width: 30, height: 30))
paddingView.addSubview(imageView)
textField.leftView = paddingView
}
func setupViews() {
view.addSubview(myMapView)
myMapView.topAnchor.constraint(equalTo: view.topAnchor).isActive=true
myMapView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive=true
myMapView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive=true
myMapView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60).isActive=true
self.view.addSubview(txtFieldSearch)
txtFieldSearch.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive=true
txtFieldSearch.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive=true
txtFieldSearch.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive=true
txtFieldSearch.heightAnchor.constraint(equalToConstant: 35).isActive=true
setupTextField(textField: txtFieldSearch, img: #imageLiteral(resourceName: "map_Pin"))
restaurantPreviewView=RestaurantPreviewView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 190))
self.view.addSubview(btnMyLocation)
btnMyLocation.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30).isActive=true
btnMyLocation.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive=true
btnMyLocation.widthAnchor.constraint(equalToConstant: 50).isActive=true
btnMyLocation.heightAnchor.constraint(equalTo: btnMyLocation.widthAnchor).isActive=true
}
let myMapView: GMSMapView = {
let v=GMSMapView()
v.translatesAutoresizingMaskIntoConstraints=false
return v
}()
let txtFieldSearch: UITextField = {
let tf=UITextField()
tf.borderStyle = .roundedRect
tf.backgroundColor = .white
tf.layer.borderColor = UIColor.darkGray.cgColor
tf.placeholder="場所で検索"
tf.translatesAutoresizingMaskIntoConstraints=false
return tf
}()
let btnMyLocation: UIButton = {
let btn=UIButton()
btn.backgroundColor = UIColor.white
btn.setImage(#imageLiteral(resourceName: "my_location"), for: .normal)
btn.layer.cornerRadius = 5
btn.clipsToBounds=true
btn.tintColor = UIColor.white
btn.imageView?.tintColor=UIColor.white
btn.addTarget(self, action: #selector(btnMyLocationAction), for: .touchUpInside)
btn.translatesAutoresizingMaskIntoConstraints=false
return btn
}()
var restaurantPreviewView: RestaurantPreviewView = {
let v=RestaurantPreviewView()
return v
}()
}
问题是您的标记是 POI(兴趣点)标记,而不是自定义标记。您可以禁用 POI 标记上的 infoViews (already answered on Whosebug),也可以将您自己的标记添加到默认情况下不会打开 infoViews 的地图。
自定义标记使用引用 marker
的委托方法,而您引用的是 restaurantMarker
:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
...
}
每当点击 GMSMarker
时,您都会在 GMSMapViewDelegate's
方法中获得回调:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
默认情况下,当点击 GMSMarker
时,会弹出该标记的 infoWindow
。如果您希望标记点击时发生其他事情,请将其添加到 delegate
方法和 return true
.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
{
//Your custom logic to move to another VC
return true
}
你的问题陈述我不是很清楚。如果您仍然遇到任何问题,请澄清。
我是 Swift 的初学者,是一名来自日本的学生。所以我的英语不好。但是请阅读我的代码并给我解决方案。
我想在点击 GMSMarker 时弹出详细视图 但是,当我点击 GMSMarker 时,将调用 infoView,然后点击 infoView,详细视图显示在此代码上。 我该如何修改这个? GMSMarker 没有 infoView 是不是可以直接进入详细视图??
请帮帮我。
//MARK: textfield
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let autoCompleteController = GMSAutocompleteViewController()
autoCompleteController.delegate = self
let filter = GMSAutocompleteFilter()
autoCompleteController.autocompleteFilter = filter
self.locationManager.startUpdatingLocation()
self.present(autoCompleteController, animated: true, completion: nil)
return false
}
// MARK: GOOGLE AUTO COMPLETE DELEGATE
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let lat = place.coordinate.latitude
let long = place.coordinate.longitude
showPartyMarkers(lat: lat, long: long)
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)
myMapView.camera = camera
txtFieldSearch.text=place.formattedAddress
chosenPlace = restaurant(name: place.formattedAddress!, lat: lat, long: long)
let restaurantMarker=GMSMarker()
restaurantMarker.position = CLLocationCoordinate2D(latitude: lat, longitude: long)
restaurantMarker.title = "\(place.name)"
restaurantMarker.snippet = "\(place.formattedAddress!)"
restaurantMarker.map = myMapView
self.dismiss(animated: true, completion: nil) // dismiss after place selected
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("ERROR AUTO COMPLETE \(error)")
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
self.dismiss(animated: true, completion: nil)
}
func initGoogleMaps() {
let camera = GMSCameraPosition.camera(withLatitude: 28.7041, longitude: 77.1025, zoom: 17.0)
self.myMapView.camera = camera
self.myMapView.delegate = self
self.myMapView.isMyLocationEnabled = true
}
// MARK: CLLocation Manager Delegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while getting location \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.delegate = nil
locationManager.stopUpdatingLocation()
let location = locations.last
let lat = (location?.coordinate.latitude)!
let long = (location?.coordinate.longitude)!
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)
self.myMapView.animate(to: camera)
showPartyMarkers(lat: lat, long: long)
}
// MARK: GOOGLE MAP DELEGATE restaurantMarkerがタップされた時の動き
func mapView(_ mapView: GMSMapView, didTap restaurantMarker: GMSMarker) -> Bool {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return false }
let img = customMarkerView.img!
let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: img, borderColor: UIColor.white, tag: customMarkerView.tag)
restaurantMarker.iconView = customMarker
return false
}
func mapView(_ mapView: GMSMapView, markerInfoContents restaurantMarker: GMSMarker) -> UIView? {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return nil }
let data = previewDemoData[customMarkerView.tag]
restaurantPreviewView.setData(title: data.title, img: data.img, price: data.price)
return restaurantPreviewView
}
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf restaurantMarker: GMSMarker) {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return }
let tag = customMarkerView.tag
restaurantTapped(tag: tag)
}
func mapView(_ mapView: GMSMapView, didCloseInfoWindowOf restaurantMarker: GMSMarker) {
guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return }
let img = customMarkerView.img!
let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: img, borderColor: UIColor.darkGray, tag: customMarkerView.tag)
restaurantMarker.iconView = customMarker
}
//マーカーがどう動くか
func showPartyMarkers(lat: Double, long: Double) {
myMapView.clear()
for restaurant in restaurants {
let restaurantMarker=GMSMarker()
let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: previewDemoData[0].img, borderColor: UIColor.darkGray, tag: 0)
//[0]の部分をiにしてデータから順に引張てくるようにすれば良い
restaurantMarker.position = CLLocationCoordinate2D(latitude: restaurant.lat, longitude: restaurant.long)
restaurantMarker.iconView=customMarker
restaurantMarker.map = self.myMapView
}
}
@objc func btnMyLocationAction() {
let location: CLLocation? = myMapView.myLocation
if location != nil {
myMapView.animate(toLocation: (location?.coordinate)!)
}
}
@objc func restaurantTapped(tag: Int) {
let v=DetailsVC()
v.passedData = previewDemoData[tag]
self.navigationController?.pushViewController(v, animated: true)
}
func setupTextField(textField: UITextField, img: UIImage){
textField.leftViewMode = UITextFieldViewMode.always
let imageView = UIImageView(frame: CGRect(x: 5, y: 5, width: 20, height: 20))
imageView.image = img
let paddingView = UIView(frame:CGRect(x: 0, y: 0, width: 30, height: 30))
paddingView.addSubview(imageView)
textField.leftView = paddingView
}
func setupViews() {
view.addSubview(myMapView)
myMapView.topAnchor.constraint(equalTo: view.topAnchor).isActive=true
myMapView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive=true
myMapView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive=true
myMapView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60).isActive=true
self.view.addSubview(txtFieldSearch)
txtFieldSearch.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive=true
txtFieldSearch.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive=true
txtFieldSearch.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive=true
txtFieldSearch.heightAnchor.constraint(equalToConstant: 35).isActive=true
setupTextField(textField: txtFieldSearch, img: #imageLiteral(resourceName: "map_Pin"))
restaurantPreviewView=RestaurantPreviewView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 190))
self.view.addSubview(btnMyLocation)
btnMyLocation.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30).isActive=true
btnMyLocation.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive=true
btnMyLocation.widthAnchor.constraint(equalToConstant: 50).isActive=true
btnMyLocation.heightAnchor.constraint(equalTo: btnMyLocation.widthAnchor).isActive=true
}
let myMapView: GMSMapView = {
let v=GMSMapView()
v.translatesAutoresizingMaskIntoConstraints=false
return v
}()
let txtFieldSearch: UITextField = {
let tf=UITextField()
tf.borderStyle = .roundedRect
tf.backgroundColor = .white
tf.layer.borderColor = UIColor.darkGray.cgColor
tf.placeholder="場所で検索"
tf.translatesAutoresizingMaskIntoConstraints=false
return tf
}()
let btnMyLocation: UIButton = {
let btn=UIButton()
btn.backgroundColor = UIColor.white
btn.setImage(#imageLiteral(resourceName: "my_location"), for: .normal)
btn.layer.cornerRadius = 5
btn.clipsToBounds=true
btn.tintColor = UIColor.white
btn.imageView?.tintColor=UIColor.white
btn.addTarget(self, action: #selector(btnMyLocationAction), for: .touchUpInside)
btn.translatesAutoresizingMaskIntoConstraints=false
return btn
}()
var restaurantPreviewView: RestaurantPreviewView = {
let v=RestaurantPreviewView()
return v
}()
}
问题是您的标记是 POI(兴趣点)标记,而不是自定义标记。您可以禁用 POI 标记上的 infoViews (already answered on Whosebug),也可以将您自己的标记添加到默认情况下不会打开 infoViews 的地图。
自定义标记使用引用 marker
的委托方法,而您引用的是 restaurantMarker
:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
...
}
每当点击 GMSMarker
时,您都会在 GMSMapViewDelegate's
方法中获得回调:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
默认情况下,当点击 GMSMarker
时,会弹出该标记的 infoWindow
。如果您希望标记点击时发生其他事情,请将其添加到 delegate
方法和 return true
.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
{
//Your custom logic to move to another VC
return true
}
你的问题陈述我不是很清楚。如果您仍然遇到任何问题,请澄清。