Chaudhry Talha
Chaudhry Talha

Reputation: 7797

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

I've this gif image:
enter image description here
I am able to add a custom static image as marker using the following code:

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

I want to use this gif image as marker. Is there anyway? Though I know static images are encouraged but is there a possibility?

I was thinking of adding a UIView say a 100x100 view and animating this icon in it, haven't tried it but I tried the solution mentioned in this thread: How to load GIF image in Swift? as:

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
}

but doing so also doesn't work. I've added this gif image in assets and this is how it appears in it:

enter image description here

Upvotes: 2

Views: 1422

Answers (2)

Thank for your question and answer. Main idea here is overwrite GMSMarker.iconView by using UIImageView that display Gif Image

I used your idea for my implementations which may be a little complex than your answer but not use 3rd library

Firstly, I export gif image to sequence of png images by this tool https://onlinepngtools.com/convert-gif-to-png

Then I create UIImageView that animate those 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];

This code uses objective c. But I think It may give additional benefit for others about this topic

Upvotes: 0

Chaudhry Talha
Chaudhry Talha

Reputation: 7797

I've got it. So here is the solution:

  1. Add the gif image in project folder not in assets. enter image description here
  2. Make a new swift file and copy/paste the code from this Link: https://github.com/kiritmodi2702/GIF-Swift/blob/master/GIF-Swift/iOSDevCenters%2BGIF.swift
  3. Add this code where you want to have the gif image. In my case I'm placing it at user location:

    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
    } }
    

and it works: enter image description here

Upvotes: 1

Related Questions