13

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)
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Shilpashree MC
  • 611
  • 1
  • 6
  • 16

5 Answers5

17
// 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)
        }
    }
Sandy Patel
  • 768
  • 7
  • 19
Shilpashree MC
  • 611
  • 1
  • 6
  • 16
12

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)
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Hugo Jordao
  • 786
  • 8
  • 9
5

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

You can easily find the coordinates of Map's center using this :

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

Or this

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 by using Geocoding API by google.

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

You can refresh Address when this delegate method is called :

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

Hope this helps.

Bharat
  • 340
  • 1
  • 8
1

Based from Release Notes made in Maps SDK for IOS, changing a markers position will cause the marker to animate to the new location.

To resolve this, you may use new features for GMSMarker as stated in Release Version 1.5 such as:

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

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

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22
  • @Teyam.. I have small doubt. i am working with google maps. when location updating, my map view also animating.Please help me. – Uma Madhavi Dec 17 '16 at 10:14
0
    objc_sync_enter(self)
    CATransaction.begin()
    CATransaction.setCompletionBlock { [weak self] in
        guard let `self` = self else { return }
        DispatchQueue.main.delay(0.5) {
            self.mapViewService.setCenter(location.coordinate, animate: true, zoom: nil)
        }
        objc_sync_exit(self)
    }
    CATransaction.setAnimationDuration(vm.continunousLocationUpdateTimeInterval)
    self.mapViewService.moveMarker(position: location.coordinate, marker: userMarker)
    CATransaction.commit()

the code above is useful to me, the moveMarker method just packaged the code marker.position = newCoordinate