【问题标题】:GIF Image as Marker in Google Map for iOS (Swift)GIF 图像作为 iOS 版 Google 地图中的标记(Swift)
【发布时间】:2019-02-13 22:31:01
【问题描述】:

我有这个 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 视图并在其中设置此图标的动画,尚未尝试但我尝试了此线程中提到的解决方案:How to load GIF image in Swift? 为:

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 图像,它是这样显示的:

【问题讨论】:

    标签: ios swift google-maps google-maps-markers cllocationmanager


    【解决方案1】:

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

    1. 在项目文件夹中添加 gif 图像,而不是在资产中。
    2. 创建一个新的 swift 文件并复制/粘贴此链接中的代码: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
      } }
      

    它的工作原理:

    【讨论】:

      【解决方案2】:

      感谢您的提问和回答。这里的主要思想是使用显示 Gif 图像的 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];
      

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

      【讨论】:

      • 感谢您分享这种方法 :) 我相信它会对很多人有用。
      猜你喜欢
      • 1970-01-01
      • 2013-03-27
      • 2018-08-01
      • 2015-09-26
      • 2011-09-27
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 2011-08-24
      相关资源
      最近更新 更多