[swift] GoogleMap Markerをドラッグ可能にする

GoogleMap Markerをドラッグ可能にする

GoogleMap Markerをドラッグ可能にすることができるが
Swift版のdidBeginDraggingMarker、didEndDraggingMarker、didDragMarkerの資料がなかったので記述する

マーカーのドラッグはマーカーを長押しするとドラッグできるようになる

var latitude: CLLocationDegrees = 1.304843 //Singapore
var longitude: CLLocationDegrees = 103.831824 //Singapore
var googleMap : GMSMapView!

override func viewDidLoad() {
        super.viewDidLoad()
        
        let width = self.view.frame.maxX
        let height = self.view.frame.maxY
        let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(latitude,longitude: longitude, zoom: kCourseMapZoom) // カメラを生成.
        googleMap = GMSMapView(frame: CGRectMake(0, 0, width, height)) // MapViewを生成.
        googleMap.mapType = kGMSTypeNormal //MAPtype標準
        googleMap.delegate = self
        googleMap.camera = camera // MapViewにカメラを追加.
        googleMap.myLocationEnabled = true //現在位置を有効化
        self.view.addSubview(googleMap)//viewにMapViewを追加

        let marker: GMSMarker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(latitude, longitude)
        marker.draggable = true //markerをドラッグ可能に
    }


    //ドラッグ開始
    func mapView(mapView: GMSMapView, didBeginDraggingMarker marker: GMSMarker){
        print("didBeginDraggingMarker--------------")
        marker.opacity = 0.4 //ドラッグ中は対象マーカーを透明度0.4に        
    }
    
    //ドラッグ終了
    func mapView(mapView: GMSMapView, didEndDraggingMarker marker: GMSMarker){
        print("didEndDraggingMarker--------------")
        marker.opacity = 1 //ドラッグ終了後は透明度を戻す
    }
    
    //ドラッグ中
    func mapView(mapView: GMSMapView, didDragMarker marker: GMSMarker){
        print("didDragMarker--------------")
        
    }

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です