Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long press in Google Maps

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.

like image 641
ildar1989 Avatar asked Sep 09 '17 22:09

ildar1989


2 Answers

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)
    }
}
like image 97
Gaurav Chandarana Avatar answered Oct 22 '22 10:10

Gaurav Chandarana


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
        }
    }
like image 3
Reinier Melian Avatar answered Oct 22 '22 12:10

Reinier Melian