Google 地图中作为标记的 GIF 图像 iOS (Swift)

GIF Image as Marker in Google Map for iOS (Swift)

我有这张 gif 图片:

我可以使用以下代码添加自定义静态图像作为标记:

if let location = locationManager.location {
            currentLocationMarker = GMSMarker(position: location.coordinate)
            currentLocationMarker?.icon = UIImage(named: "user")
            currentLocationMarker?.map = mapView
            currentLocationMarker?.rotation = locationManager.location?.course ?? 0
        }

我想用这个 gif 图像作为标记。到底有没有虽然我知道鼓励静态图像,但有可能吗?

我正在考虑添加一个 UIView 说一个 100x100 视图并在其中制作这个图标的动画,还没有尝试过,但我尝试了这个线程中提到的解决方案: 如:

let jeremyGif = UIImage.gifImageWithName("puppy")
let imageView = UIImageView(image: jeremyGif)
imageView.frame = CGRect(x: 0, y: 0, width: jeremyGif?.size.width ?? 100.0, height: jeremyGif?.size.height ?? 100.0)
//view.addSubview(imageView)
//mapView.addSubview(imageView)

if let location = locationManager.location {
    currentLocationMarker = GMSMarker(position: location.coordinate)
    currentLocationMarker?.icon = imageView.image //UIImage(named: "user")
    currentLocationMarker?.map = mapView
    currentLocationMarker?.rotation = locationManager.location?.course ?? 0
}

但这样做也行不通。我在资产中添加了这张 gif 图片,它是这样显示的:

我明白了。所以这是解决方案:

  1. 将 gif 图片添加到项目文件夹中,而不是资产中。
  2. 创建一个新的 swift 文件和 copy/paste 来自这个 Link 的代码:https://github.com/kiritmodi2702/GIF-Swift/blob/master/GIF-Swift/iOSDevCenters%2BGIF.swift
  3. 在您想要 gif 图像的位置添加此代码。在我的例子中,我将它放在用户位置:

    func addCurrentLocationMarker() {
    currentLocationMarker?.map = nil; currentLocationMarker = nil
    let puppyGif = UIImage.gifImageWithName("Puppy")
    let imageView = UIImageView(image: puppyGif)
    imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    
    if let location = locationManager.location {
        currentLocationMarker = GMSMarker(position: location.coordinate)
        currentLocationMarker?.iconView = imageView
        currentLocationMarker?.map = mapView
        currentLocationMarker?.rotation = locationManager.location?.course ?? 0
    } }
    

并且有效:

感谢您的提问和回答。这里的主要思想是使用显示 Gif Image

的 UIImageView 覆盖 GMSMarker.iconView

我将你的想法用于我的实现,这可能比你的答案有点复杂,但没有使用第三个库

首先,我通过这个工具将gif图片导出为png图片序列https://onlinepngtools.com/convert-gif-to-png

然后我创建 UIImageView 来为这些 PNG 设置动画

UIImageView * gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
gifImageView.animationImages = @[ [UIImage imageNamed:@"gifToPngFrame01"]
                                  ,[UIImage imageNamed:@"gifToPngFrame02"]
                                  ,[UIImage imageNamed:@"gifToPngFrame03"]
                                  ,[UIImage imageNamed:@"gifToPngFrame04"]
                                  ,[UIImage imageNamed:@"gifToPngFrame05"]
                                  ,[UIImage imageNamed:@"gifToPngFrame06"]
                                 ,[UIImage imageNamed:@"gifToPngFrame07"]
                                 ,[UIImage imageNamed:@"gifToPngFrame08"]
                                 ,[UIImage imageNamed:@"gifToPngFrame09"]
                                 ,[UIImage imageNamed:@"gifToPngFrame10"]
                                 ,[UIImage imageNamed:@"gifToPngFrame11"]
                                 ,[UIImage imageNamed:@"gifToPngFrame12"]
                                 ,[UIImage imageNamed:@"gifToPngFrame13"]];

gifImageView.animationRepeatCount = 0;
gifImageView.animationDuration = 1;


CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(21.033333, 105.87);

GMSMarker *gifMarker = [GMSMarker markerWithPosition:position2];
gifMarker.iconView = gifImageView;
gifMarker.map = mapView;
gifMarker.groundAnchor = CGPointMake(0.5, 0.5);

[mapView animateToLocation:position2];

[gifImageView startAnimating];

此代码使用 objective c。但我认为它可能会给其他人带来关于这个话题的额外好处