且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS 中 Google Map Marker 上的弹跳动画 ??[目标-c]

更新时间:2022-10-15 08:48:20

I wanted to add marker on Google map which will animate to indicated the current user. I was not able to get exact bounce animation like the link above i mentioned , for alternate way to highlight marker i did with using scale animation.
like this ...

To get animation like this . do like this

 GMSMarker *markerUser;
 NSTimer *timer;
 int imageScale;

Add Marker on Map

 imageScale=15;

    CLLocationCoordinate2D position = CLLocationCoordinate2DMake(Userlat, Userlng);
    markerUser = [GMSMarker markerWithPosition:position];
    markerUser.title = @"You are here";
    markerUser.icon=[self image:[UIImage imageNamed:@"marker_user.png"] scaledToSize:CGSizeMake(25.0f,40.0f)];   // Initial Marker Size
    markerUser.appearAnimation = kGMSMarkerAnimationPop;
    markerUser.infoWindowAnchor = CGPointMake(0.44f, 0.30f);
    markerUser.map = mapView_;

Start Timer when marker is added on map, and change the icon of marker with different size .

 timer =  [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(targetMethod:)
                                   userInfo:nil
                                    repeats:YES];

at every 0.1 seconds tragetMethod will be fired , here you can scale the icon image and reassign to marker icon

-(void)targetMethod:(NSTimer *)timer {

    if (imageScale<30) {

        markerUser.icon=[self image:[UIImage imageNamed:@"marker_user.png"] scaledToSize:CGSizeMake(imageScale,imageScale*1.5)];

        imageScale+=1;

    }
    else{
        imageScale=15;

         markerUser.icon=[self image:[UIImage imageNamed:@"marker_user.png"] scaledToSize:CGSizeMake(imageScale,imageScale*1.5)];
    }

}

and here is the method that will scale your UIImage

- (UIImage *)image:(UIImage*)originalImage scaledToSize:(CGSize)size
{
    //avoid redundant drawing
    if (CGSizeEqualToSize(originalImage.size, size))
    {
        return originalImage;
    }

    //create drawing context
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);

    //draw
    [originalImage drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];

    //capture resultant image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return image
    return image;
}

This may solve problem of guys looking for such animation.