0

Here I'm working on mapKitView using GoogleMapKit and here i want to do when the user drag the marker by using of didBeginDragging, didDragging and didEndDragging and release the marker at any location point of the screen. I found a helping content to fixed the marker in screen from Hugo Jordao's answer, but this only works to fixed marker position in the center of the screen only.So how can i set marker at any specific screen point??

  • Dragging Method
    func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
        if marker == mapMarker{
            self.markerPosition = GMSCameraPosition(latitude: marker.position.latitude, longitude: marker.position.longitude, zoom: 5.0)
            print("End Dragging")
        }
    }
  • Set Marker center to the Screen
 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
     if self.markerPosition == nil{
         mapMarker.position = position.target
     }else{
         mapMarker.position = self.markerPosition.target
     }
 }
steveSarsawa
  • 1,559
  • 2
  • 14
  • 31

1 Answers1

0

Answering my own question.

First added variable to store the location value in CGPoint

var markerPosition : CGPoint!
  • Then convert CLLocationCoordinate2D Location values into the CGPoint and store into markerPosition in didEndDragging marker method.
 func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
        if marker == mapMarker{

            self.markerPosition = self.mapView.projection.point(for: CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude))

            print("End Dragging")
        }
    }
  • After that set GMSMarker position at any screen point as per CGPoint values into the MAPView
 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
     if self.markerPosition == nil{
         //To set pin into the center of mapview
         mapMarker.position = position.target 

     }else{
         //To set pin into the any particular point of the screen on mapview
         let coordinate = self.mapView.projection.coordinate(for: self.markerPosition)
         mapMarker.position = coordinate
     }
 }
steveSarsawa
  • 1,559
  • 2
  • 14
  • 31