7

According to documentation you can change marker position with animation:

Marker position. Animated.

Do you know how to disable this animation?

Roman Barzyczak
  • 3,785
  • 1
  • 30
  • 44

3 Answers3

7

Suppose your reference for marker is mainMarker which is a GMSMarker object

var mainMarker:GMSMarker?

And suppose this is your function to change marker position without animation

func changeMarkerWithoutAnimation() {
    mainMarker?.map = nil
    mainMarker = nil
    let changedPosition = CLLocationCoordinate2DMake(22.9734, 78.6569)
    mainMarker = GMSMarker(position: changedPosition)
    mainMarker?.title = "Hello World"
    mainMarker!.map = mapView
}

This will change your marker's position without animation.

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
  • @RajanMaheshwari.. I want to move my marker with my updated coordinates .How can i achieve this. – Uma Madhavi Dec 02 '16 at 12:37
  • @UmaMadhavi This is just creating a new marker with updated coordinates without animation. You can simply use `CoreAnimation` to achieve this and not making your marker nil. Check this http://stackoverflow.com/a/19128891/2545465 Even the google maps also provide a method `markerWithPosition:` for that purpose – Rajan Maheshwari Dec 03 '16 at 09:37
  • @RajanMaheshwari.. yes, but i didn't understand that one. – Uma Madhavi Dec 03 '16 at 09:38
  • @UmaMadhavi Its simple, first of all where you get your coordinates, may be in `didUpdateLocation`. Wherever you get, animate the marker towards your new coordinates. Just provide marker the new coords and I guess by default it animates to that position in Google Maps – Rajan Maheshwari Dec 03 '16 at 09:40
  • @RajanMaheshwari.yes i want to add custom image for example car & i want to rotate that car icon not the default one. – Uma Madhavi Dec 03 '16 at 09:42
  • @UmaMadhavi There is a property in `GMSMarker` called `rotation`. https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_marker.html#abaa6d5cd12a00dad6388019f7913ebf7. If you want to change the default marker image you can set a custom icon, using the marker's `icon` or `iconView` property. Read the documentation. Everything is there. https://developers.google.com/maps/documentation/ios-sdk/marker – Rajan Maheshwari Dec 03 '16 at 11:33
3

As implemented in google maps samples, you should use CATransaction for this purpose.

let changedPosition = CLLocationCoordinate2DMake(22.9734, 78.6569)
CATransaction.begin()
CATransaction.setAnimationDuration(0.0)
marker.position = changedPosition
CATransaction.commit()

Source demo.

1

Another dirty and hacky workaround without recreating the marker:

extension GMSMarker
{
    func moveWithoutAnimation(_ newPosition: CLLocationCoordinate2D)
    {
        guard let map = map else {
            return
        }

        let oldPoint = map.projection.point(for: position)
        let newPoint = map.projection.point(for: newPosition)
        let delta = CGPoint(x: newPoint.x - oldPoint.x, y: newPoint.y - oldPoint.y)
        let width = iconView?.width ?? icon?.size.width ?? 26
        let height = iconView?.height ?? icon?.size.height ?? 41
        let deltaInPercentage = CGPoint(x: 0.5 - delta.x/width, y: 0.5 - delta.y/height)

        groundAnchor = deltaInPercentage
    }
}

Note, that actual marker.position won't change.

Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51