默认手势事件不在谷歌地图工作
问题描述:
我想在长按新增标记,但我的didLongPressAt坐标事件不起作用。我用它的打印语句进行测试。 我已经在其中添加了UIView。我也使用内置手势,但仍然无法工作。 这是我的代码。请帮助我。默认手势事件不在谷歌地图工作
var mapView=GMSMapView()
var camera = GMSCameraPosition()
let locationmanager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate=self
// Do any additional setup after loading the view.
self.locationmanager.delegate=self
self.locationmanager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters
self.locationmanager.requestWhenInUseAuthorization()
self.locationmanager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationmanager.stopUpdatingLocation()
}
func showCurrentLocationOnMap()
{
camera = GMSCameraPosition.camera(withLatitude: (self.locationmanager.location?.coordinate.latitude)!, longitude: (self.locationmanager.location?.coordinate.longitude)!, zoom: 15)
mapView = GMSMapView.map(withFrame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(myView.frame.size.width), height: CGFloat(myView.frame.size.height)), camera: camera)
mapView.settings.myLocationButton=true
mapView.isMyLocationEnabled=true
let marker = GMSMarker()
marker.position=camera.target
marker.snippet="My Current Location"
marker.appearAnimation=GMSMarkerAnimation.pop
marker.map=mapView
myView.addSubview(mapView)
}
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
print (coordinate.latitude)
print(coordinate.longitude)
}
答
这里的问题是,你这样做,你在这里创建未设置为自mapView = GMSMapView.map(withFrame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(myView.frame.size.width), height: CGFloat(myView.frame.size.height)), camera: camera)
新GMSMapView中实例的代表再次初始化的MapView。因此代理方法(didLongPressAt
)未被调用。
取而代之的是,如果您只是想在当前位置有机会时重新定位相机,请使用mapView.animate(with cameraUpdate: GMSCameraUpdate)
。
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: (self.locationmanager.location?.coordinate.latitude)!, longitude: (self.locationmanager.location?.coordinate.longitude)!, zoom: 15)
mapView = GMSMapView.map(withFrame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(myView.frame.size.width), height: CGFloat(myView.frame.size.height)), camera: camera)
self.mapView.delegate=self
// Do any additional setup after loading the view.
self.locationmanager.delegate=self
self.locationmanager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters
self.locationmanager.requestWhenInUseAuthorization()
self.locationmanager.startUpdatingLocation()
}
func showCurrentLocationOnMap()
{
let newLocation = CLLocationCoordinate2D(latitude: self.locationmanager.location?.coordinate.latitude, self.locationmanager.location?.coordinate.longitude: longitude)
let cameraPositionUpdate = GMSCameraUpdate.setTarget(newLocation)
self.mapView.animate(with: cameraPositionUpdate)
// Code to add the marker
}
mapView.animate(with cameraUpdate:GMSCameraUpdate)它不起作用。它显示错误无法调用动画 –
您需要创建一个GMSCameraUpdate实例,然后将其传递给此方法。我上面提到的代码只是一个原型。 – dRAGONAIR
您需要在您的“showCurrentLocationOnMap” 'let newLocation = CLLocationCoordinate2D(纬度:self.locationmanager.location?.coordinate.latitude,self.locationmanager.location?.coordinate.longitude:longitude) let cameraPositionUpdate = GMSCameraUpdate.setTarget(newLocation) self.mapView.animate(with:cameraPositionUpdate)' – dRAGONAIR