How to move marker with moving Google Map in iOS?

I display a marker in a specific location along with the current address on 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 move the map, I have to move the marker along with the map at the same time, and I have to display the address of this location in the address label.

How can i do this?

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) } } 
+5
source share
4 answers
 // 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) } } 
+8
source

There is one trick that can help you here. Instead of using GMSMarker here, put the image pointing to the center through your Google MapView.

You can easily find the coordinates of the center of the map using this:

 double latitude = mapView.camera.target.latitude; double longitude = mapView.camera.target.longitude; 

Or that

 GMSCoordinateBounds *bounds = nil; bounds = [[GMSCoordinateBounds alloc] initWithRegion: visibleRegion]; CLLocationCoordinate2D centre = CLLocationCoordinate2DMake( (bounds.southWest.latitude + bounds.northEast.latitude) / 2, (bounds.southWest.longitude + bounds.northEast.longitude) / 2); 

Now you can get the location address using the Google geocoding API.

Here is the link: https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_geocoder

You can update the address when this delegate method is called:

 - (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position 

Hope this helps.

+2
source

Based on the Release Notes made in the Maps SDK for iOS, changing the position of the markers will revive the marker in the new location.

To solve this problem, you can use the new features for GMSMarker, as indicated in Release Version 1.5 , for example:

  • Markers can be dragged using the draggable property, and new drag delegation methods have been added to the GMSMapViewDelegate. (Issue 4975).
  • Added GMSMarkerLayer - a custom subclass of CALayer for GMSMarker that supports animation of marker position and rotation. (Issue 4951, Issue 5743).

In addition to this, this GitHub post - GoogleMapsAnimationGlitch and this SO post - How to smoothly move the GMSMarker along coordinates in Objective-c can also help.

+1
source

Update for Swift 4:

First you need to match the GMSMapViewDelegate :

 extension MapsVC: GMSMapViewDelegate{ 

And then set the VC as the viewMap delegate to viewDidLoad ()

 viewMap.delegate = self 

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

 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) { destinationMarker.position = position.target print(destinationMarker.position) } 
+1
source

All Articles