2017-04-22 14 views
0
私は長押し上のマーカーを追加したい

で作業されていませんが、私のdidLongPressAt座標イベントが機能していません。私はテストのためにその中でprintステートメントを使用しました。 UIViewが追加されました。私はジェスチャーも組み込みましたが、まだ動作していません。 ここに私のコードです。私を助けてください。デフォルトのジェスチャーイベントは、Googleマップ

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

答えて

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) 新しい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 
} 
+0

mapView.animate(with cameraUpdate:GMSCameraUpdate)は機能しません。エラーがアニメーションを呼び出せないことを示しています –

+0

GMSCameraUpdateのインスタンスを作成し、このメソッドに渡す必要があります。上記のコードは単なるプロトタイプでした。 – dRAGONAIR

+0

あなたはあなたの "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) ' – dRAGONAIR

関連する問題