Reputation: 7797
I've this gif image:
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:
Upvotes: 2
Views: 1422
Reputation: 4518
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
Reputation: 7797
I've got it. So here is the solution:
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
} }
Upvotes: 1