简体   繁体   中英

Do marker animations exist on GoogleMaps SDK for iOS?

是否可以通过动画在GMSMapView上移动/旋转GMSMarker

The addition in 1.2 is that the GMSMarker class has an animated property - I presume you just set it to YES , before adding the marker to the map by settings its map property (I haven't tried it though).

https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_marker

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.animated = YES;
marker.map = mapView_;

I presume this means that the marker will be animated when it is dropped onto the map - not that you can make a high-speed animated marker like this original question was asking.

No I'm afraid they are not as we have no access to the OpenGL context that Google Maps has access to. The best you can do is rotate a marker as a UIImage which requires a redraw or you can move a marker but it will jump unless you do it in very small increments!

I suggest reporting a bug to Google and they may be able to include it

My solution was to subclass the GMSMarker class and add support for PNG sequences using a timer. Here's a rough sketch of the code:

.h:

#import <GoogleMaps/GoogleMaps.h>

@interface AnimatedGMSMarker : GMSMarker

@property (nonatomic, strong) NSString *animationBaseName;

-(void)setAnimation:(NSString *)name forFrames:(NSArray *)frames;

@end

.m

#import "AnimatedGMSMarker.h"

@implementation AnimatedGMSMarker{
    int _currentFrame;
    NSArray *_frameArray;
    NSTimer *_timer;
}

-(void)setAnimation:(NSString *)name forFrames:(NSArray *)frames{
    _frameArray = frames;
    _currentFrame = 0;
    _animationBaseName = name;
    self.icon = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",_animationBaseName,_frameArray[_currentFrame++]]];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0/24.0f
                                                     target:self
                                                   selector:@selector(onRefreshTimer:)
                                                   userInfo:nil
                                                    repeats:YES];
}

-(void)onRefreshTimer:(NSTimer *)timer{
    self.icon = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",_animationBaseName,_frameArray[_currentFrame++]]];
    if (_currentFrame >= _frameArray.count){
        _currentFrame = 0;
    }
}

@end

And then once you instantiate one, just send it something like this:

[self.myAnimatedMarker setAnimation:@"some_library_name" forFrames:@[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9]];

And make sure your library has a bunch of PNGs of the same size and registration that are named "some_library_name0" "some_library_name1", etc. The frame array treatment allows you to repeat frames without creating new PNGs.

Performance-wise, it's very slow to animate in the simulator but seems pretty performant on the device.

Good luck!

For now markers only have an option to appear animated.

Recently wrote clusterization lib for Google Maps SDK for iOS with animated collapse/disintegration. And the approach I used was animation through manual position update. If there's a lot markers on screen simultaneously animating it's sloow (even with all the possible calculations in background and results caching), so needs a lot of optimizations and limitations. So for now it's good to think a lot if you really need animations like this with Google Maps SDK for iOS or sometimes, especially on older devices, the optimization you'll have to use will be disabling your custom animations at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM