Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
    }
}
like image 916
Shilpashree MC Avatar asked Jun 23 '16 12:06

Shilpashree MC


People also ask

How do I move a marker smoothly on Google Maps?

The initialize() function creates a Google Map with a marker. The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

How do you drag a marker on Google Maps?

You can allow users to move a marker on the map by setting the marker's draggable property to true .

How do I drag a marker on Google Maps in Swift?

You have to press-and-hold on the marker before it will begin dragging. Save this answer. Show activity on this post.


1 Answers

Update for Swift 4:

First you need to conform with the GMSMapViewDelegate:

extension MapsVC: GMSMapViewDelegate{

And then set your VC as the delegate of viewMap in the viewDidLoad()

viewMap.delegate = self

After that you just need to use the following method to get updates from the camera position and set that as the new position for the marker:

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    destinationMarker.position = position.target
    print(destinationMarker.position)
}
like image 151
Hugo Jordao Avatar answered Sep 29 '22 23:09

Hugo Jordao