Help me solve the problem. I'm trying to track a long click on the map in Google Maps but I can not do it. Here is an example of my code:
import UIKit
import GoogleMaps
class ViewController: UIViewController {
@IBOutlet var mMap: GMSMapView!
var longPressRecognizer = UILongPressGestureRecognizer()
@IBAction func longPress(_ sender: UILongPressGestureRecognizer) {
testTextview.text = "You tapped at YES"
}
override func viewDidLoad() {
super.viewDidLoad()
longPressRecognizer = UILongPressGestureRecognizer(target: self,
action: #selector(self.longPress))
longPressRecognizer.minimumPressDuration = 0.5
mMap.addGestureRecognizer(longPressRecognizer)
mMap.isMyLocationEnabled = true
mMap.settings.compassButton = true
mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200,
longitude: 52.3154000, zoom: 15.0)
}
}
Using this code does not happen. I tried all the methods that were on stackoverflow but also nothing happened.
Just assign your viewController class as a delegate of GMSMapViewDelegate.
override func viewDidLoad() {
super.viewDidLoad()
self.mMap.delegate = self
//....
}
and track long press in the following method...
extension ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
print(coordinate)
}
}
GMSMapView
class have a lot of different gestures recognisers, so you need add a UIGestureRecognizerDelegate
implementation adding this method to your viewController shouldRecognizeSimultaneouslyWith
here is what you need to modify
add
extension ViewController : UIGestureRecognizerDelegate
{
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
}
add
longPressRecognizer.delegate = self
below this line longPressRecognizer.minimumPressDuration = 0.5
Full Code
import UIKit
import GoogleMaps
class ViewController: UIViewController {
@IBOutlet var mMap: GMSMapView!
var longPressRecognizer = UILongPressGestureRecognizer()
@IBAction func longPress(_ sender: UILongPressGestureRecognizer) {
testTextview.text = "You tapped at YES"
}
override func viewDidLoad() {
super.viewDidLoad()
longPressRecognizer = UILongPressGestureRecognizer(target: self,
action: #selector(self.longPress))
longPressRecognizer.minimumPressDuration = 0.5
longPressRecognizer.delegate = self
mMap.addGestureRecognizer(longPressRecognizer)
mMap.isMyLocationEnabled = true
mMap.settings.compassButton = true
mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200,
longitude: 52.3154000, zoom: 15.0)
}
}
extension ViewController : UIGestureRecognizerDelegate
{
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With