How do I move marker along with moving of Google Map in iOS?

Shilpashree MC picture Shilpashree MC · Jun 23, 2016 · Viewed 26.3k times · Source

I am displaying a marker in a particular place, along with displaying the current address in the address label on Google Maps.

Now, I want to change the location by moving the Google Map, but the problem is that when I am moving the map, I should simultaneously move the marker along with the map, and I should display the address of that location in the address label.

How can I do that?

I tried this:

let destinationMarker = GMSMarker(position: self.destinationLocation.coordinate)

let image = UIImage(named:"sourcemarker")
destinationMarker.icon = image
destinationMarker.draggable = true
destinationMarker.map = self.viewMap
//viewMap.selectedMarker = destinationMarker
destinationMarker.title = "hi"
destinationMarker.userData = "changedestination"

func mapView(mapView: GMSMapView, didEndDraggingMarker marker: GMSMarker)
{
    if marker.userData as! String == "changedestination"
    {
        self.destinationLocation = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
        self.destinationCoordinate = self.destinationLocation.coordinate
        //getAddressFromLatLong(destinationCoordinate)
    }
}

Answer

Shilpashree MC picture Shilpashree MC · Jul 5, 2016
// UpdteLocationCoordinate
    func updateLocationoordinates(coordinates:CLLocationCoordinate2D) {
        if destinationMarker == nil
        {
            destinationMarker = GMSMarker()
            destinationMarker.position = coordinates
            let image = UIImage(named:"destinationmarker")
            destinationMarker.icon = image
            destinationMarker.map = viewMap
            destinationMarker.appearAnimation = kGMSMarkerAnimationPop
        }
        else
        {
            CATransaction.begin()
            CATransaction.setAnimationDuration(1.0)
            destinationMarker.position =  coordinates
            CATransaction.commit()
        }
    }

    // Camera change Position this methods will call every time
    func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
        let destinationLocation = CLLocation()
        if self.mapGesture == true
        {
            destinationLocation = CLLocation(latitude: position.target.latitude,  longitude: position.target.longitude)
            destinationCoordinate = destinationLocation.coordinate
            updateLocationoordinates(destinationCoordinate)
        }
    }