Roman Barzyczak
Roman Barzyczak

Reputation: 3813

How to disable animation for changing GMSMarker position in iOS?

According to documentation you can change marker position with animation:

Marker position. Animated.

Do you know how to disable this animation?

Upvotes: 7

Views: 3802

Answers (3)

Maksym Savisko
Maksym Savisko

Reputation: 51

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.

Upvotes: 5

Pavel Alexeev
Pavel Alexeev

Reputation: 6081

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.

Upvotes: 1

Rajan Maheshwari
Rajan Maheshwari

Reputation: 14571

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.

Upvotes: 7

Related Questions