12

I'm using google map sdk. I want to update gps coordinates of pin after each 5 seconds. Currently I'm just updating position attribute of GMSMarker. But it gives jump effect. I want to move marker smoothly on map.
Here is my code to update position of marker

-(void)updateLocationoordinates(CLLocationCoordinate2D) coordinates
{ 
  if (marker == nil) {
      marker = [GMSMarker markerWithPosition:coordinates];
      marker.icon = [UIImage imageNamed:CAR_FOUND_IMAGE];
      marker.map = mapView_;
  } else
  marker.position = coordinates; 
}
Sadia
  • 462
  • 1
  • 5
  • 13

3 Answers3

35

Change your else block to be something more like this:

[CATransaction begin];
[CATransaction setAnimationDuration:2.0];
marker.position = coordindates;
[CATransaction commit];

We enable you to use Core Animation for animating Google Maps.

For a worked sample, please see AnimatedCurrentLocationViewController.{c,m} in the SDK sample application.

Brett
  • 2,399
  • 1
  • 17
  • 23
  • Thank you @Brett. I updated the google sdk then these animations worked fine. – Sadia Oct 02 '13 at 06:43
  • For me, this actually didn't work. In fact, it completely broke animation of the .position property. The good news is that is *exactly* what I wanted: a way to turn off the animation of the .position property. Thanks a ton! For reference, I set the animation duration to 0.0, but having a non-0 value still didn't animate. – u2Fan Aug 06 '14 at 03:59
  • 2
    OK - I lied. It works exactly as the poster mentions. I'd just screwed up my test. I noticed it too late to edit my first comment, however. – u2Fan Aug 06 '14 at 04:11
  • 2
    Can anyone send code snippet to animate polyline of google maps. – Kumar Swamy Apr 05 '16 at 07:57
  • @Sadia can you tel mel how did you achieve this? – Ganesh Kumar Jun 14 '16 at 05:20
  • @Brett .. How can i do this in swift – Uma Madhavi Dec 03 '16 at 11:32
  • @UmaMadhavi the syntax for using Core Animation from Swift is covered in Apple's documentation. Here's a [starting point](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CoreAnimationBasics/CoreAnimationBasics.html). – Brett Dec 15 '16 at 01:59
  • @Brett, I have used this animation and it work really good. Thanks & vote up. But i see a problem which i mentioned on this post. http://stackoverflow.com/questions/41060922/gmsmarker-bouncing-around-the-corners-on-gmspolyline . can you help me in achieving animation in this situation. I have the coordinates around the corner that user has skipped. But i cant find appropriate animation which can animate on those missed points. I tried Blocks on CATransaction , CAAnimation, Basic Key frame but do not find an appropriate one. – NewStackUser Dec 16 '16 at 12:40
  • i ask what @KumarSwamy asked can anyone please share the code snippet for animating polylines – Nitish Makhija Feb 28 '17 at 10:12
1

Brett's Answer in swift -3 and 4

CATransaction.begin()
CATransaction.setAnimationDuration(2.0)
marker.position = coordindates
CATransaction.commit()
Ajumal
  • 1,048
  • 11
  • 33
0

just Initialize marker in view did load and update it when you want to do it no need of any kind of animation like

- (void)viewDidLoad {
    [super viewDidLoad];
    ..write code here
        marker = [[GMSMarker alloc] init];
}
    marker.position = CLLocationCoordinate2DMake([latitudue doubleValue], [longitude doubleValue]);
Steve Lovell
  • 2,564
  • 2
  • 13
  • 16
Asad Farooq
  • 191
  • 1
  • 13