Ios 默认手势事件在谷歌地图中不起作用
我想在长按上添加标记,但我的didLongPressAt坐标事件不起作用。我使用了其中的print语句进行测试。 我在其中添加了UIView。我也使用了内置手势,但它们仍然不起作用。 这是我的密码。请帮帮我Ios 默认手势事件在谷歌地图中不起作用,ios,swift,google-maps,Ios,Swift,Google Maps,我想在长按上添加标记,但我的didLongPressAt坐标事件不起作用。我使用了其中的print语句进行测试。 我在其中添加了UIView。我也使用了内置手势,但它们仍然不起作用。 这是我的密码。请帮帮我 var mapView=GMSMapView() var camera = GMSCameraPosition() let locationmanager = CLLocationManager() override func viewDidLoad() { super.vi
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,再次初始化mapView
您在此处创建的新GMSMapView实例的委托未设置为self。因此,不会调用委托方法(didLongPressAt
)
相反,如果您只想在当前位置出现时重新定位摄影机,请改用mapView.animate(使用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
}
动画(使用cameraUpdate:GMSCameraUpdate)它不工作。它显示了一个错误,无法调用animateyou需要创建一个GMSCameraUpdate实例,然后将其传递给此方法。我上面提到的代码只是一个原型。您需要在“showCurrentLocationOnMap”中执行类似的操作
让newLocation=CLLocationCoordinate2D(纬度:self.locationmanager.location?.coordinate.latitude,self.locationmanager.location?.coordinate.longitude:longitude)让cameraPositionUpdate=GMSCameraUpdate.setTarget(newLocation)self.mapView.animate(使用:cameraPositionUpdate)
并删除camera=GMSCameraPosition.camera(使用纬度:(self.locationmanager.location?坐标.纬度)!,经度:(self.locationmanager.location?坐标.经度)!,缩放:15)mapView=GMSMapView.map(使用frame:CGRect(x:cgloat(0),y:cgloat(0),width:cgloat(myView.frame.size.width),height:cgloat(myView.frame.size.height)),camera:camera)
line您还需要将所有这些移动到viewDidLoadcamera=GMSCameraPosition.camera(带纬度:(self.locationmanager.location?.coordination.lation)!,经度:(self.locationmanager.location?.coordinate.longitude)!,缩放:15)mapView=GMSMapView.map(带边框:CGRect(x:CGFloat(0),y:CGFloat(0),宽度:CGFloat(myView.frame.size.width),高度:CGFloat(myView.frame.size.height)),摄像头:摄像头)mapView.settings.myLocationButton=true mapView.isMyLocationEnabled=true
在此之后,您需要拥有mapView.delegate=self