长按Google地图

时间:2017-09-09 22:07:59

标签: ios iphone swift google-maps

帮我解决问题。我试图在谷歌地图上跟踪地图上的长按,但我无法做到。以下是我的代码示例:

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)
 }
}

使用此代码不会发生。我尝试了所有在stackoverflow上的方法,但也没有发生任何事情。

2 个答案:

答案 0 :(得分:2)

GMSMapView类有很多不同的手势识别器,因此您需要添加UIGestureRecognizerDelegate实现,将此方法添加到viewController shouldRecognizeSimultaneouslyWith

以下是您需要修改的内容

添加

extension ViewController : UIGestureRecognizerDelegate
{
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
    {
        return true
    }
}

添加

longPressRecognizer.delegate = self

低于此行 longPressRecognizer.minimumPressDuration = 0.5

完整代码

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
        }
    }

答案 1 :(得分:2)

只需将viewController类指定为GMSMapViewDelegate的委托,如..

    extension ViewController: GMSMapViewDelegate {
        func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
            print(coordinate)
        }
    }

并按以下方法跟踪长按..

{{1}}