Ios 谷歌地图中的长按
帮我解决这个问题。我试图在谷歌地图上追踪一个长时间的点击,但是我做不到。下面是我的代码示例:Ios 谷歌地图中的长按,ios,iphone,swift,google-maps,Ios,Iphone,Swift,Google Maps,帮我解决这个问题。我试图在谷歌地图上追踪一个长时间的点击,但是我做不到。下面是我的代码示例: import UIKit import GoogleMaps class ViewController: UIViewController { @IBOutlet var mMap: GMSMapView! var longPressRecognizer = UILongPressGestureRecognizer() @IBAction func longPress(_ sender: UILo
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上的所有方法,但什么也没发生。
GMSMapView
类有很多不同的手势识别器,因此您需要添加一个UIgestureRecognitizerDelegate
实现将此方法添加到您的viewController中应与同时识别
以下是您需要修改的内容
添加
extension ViewController : UIGestureRecognizerDelegate
{
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
}
longPressRecognizer.delegate = self
添加
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
}
}
只需将viewController类指定为GMSMapViewDelegate的委托即可
override func viewDidLoad() {
super.viewDidLoad()
self.mMap.delegate = self
//....
}
并按以下方法跟踪长按
extension ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
print(coordinate)
}
}
您好-如果从函数中删除@iAction装饰器,会发生什么情况?(您可能需要将其替换为@objc)。此方法也不需要work@ildar1989对此有何反馈?