默认手势事件在googlemap中无效

时间:2017-04-22 11:24:52

标签: ios swift google-maps

我想在长按时添加标记,但我的didLongPressAt坐标事件无效。我在其中使用了print语句进行测试。 我在其中添加了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)
}

1 个答案:

答案 0 :(得分:0)

这里的问题是您通过执行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(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
}