Move and Rotate GMSMarker smoothly along last updated GPS coordinates Swift iOS
Asked Answered
C

1

2

I'm using GMSMapView. So I added custom GMSMarker and set the image(Ex. Bike image) and animating that marker when user starts moving and changing the angle of the marker in locationManager .

Currently I'm just updating position attribute of GMSMarker. But it gives the GMSMarker jump effect. Instead of this I want to move and rotate the GMSMarker smoothly/properly into the GMSMapView.

How can i achieve this in Swift? Thanks.

   (void)updateLocationoordinates(CLLocationCoordinate2D) coordinates
    { 
     if (marker == nil) {
      marker = [GMSMarker markerWithPosition:coordinates];
      marker.icon = [UIImage imageNamed:IMAGE];
      marker.map = mapView;
    } else{
     marker.position = coordinates; 
          }
    }  
Cheju answered 15/2, 2016 at 5:10 Comment(0)
L
1

I've faced the same problem and tried many ways. At last I've came up with a solution - to take angle between previous location point and present location point and I've used an UIImageView instead of GMSMarker.

//in location update method
CGPoint anglepoint = [myMapView.projection pointForCoordinate:currLocation.coordinate];
CGFloat angle = [self getAngle:anglepoint];
if(!isnan(angle)){
     [UIView beginAnimations:nil context:nil];
     [UIView setAnimationDuration:0.8f];
     sampleImgView.transform = CGAffineTransformMakeRotation(angle);
     [sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
     [UIView commitAnimations];
     prevCurrLocation = currLocation;
}


-(CGFloat) getAngle: (CGPoint) touchedPoints
{
    CGPoint previousLocationPoint = [myMapView.projection pointForCoordinate:prevCurrLocation.coordinate];

    CGFloat x1 = previousLocationPoint.x;
    CGFloat y1 = previousLocationPoint.y;

    CGFloat x2 = touchedPoints.x;
    CGFloat y2 = touchedPoints.y;

    CGFloat x3 = x1;
    CGFloat y3 = y2;

    CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3)));
    CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));

    CGFloat angle = atanf(oppSide/adjSide);
    // Quadrant Identifiaction
    if(x2 < previousLocationPoint.x)
    {
        angle = 0-angle;
    }


    if(y2 > previousLocationPoint.y)
    {
        angle = M_PI/2 + (M_PI/2 -angle);
    }
    return angle;
}

Use this for animation:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
sampleImgView.transform = CGAffineTransformMakeRotation(angle);
[sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
[UIView commitAnimations];

Here is the sample Imageview:

sampleImgView = [[UIImageView alloc]init];
sampleImgView.center = myMapView.center;
sampleImgView.frame = CGRectMake(0,0, 25, 50);
sampleImgView.backgroundColor = [UIColor clearColor];
[myMapView addSubview:sampleImgView];

Hope this helps.

Laux answered 15/2, 2016 at 5:19 Comment(17)
If I want to roate the direction of the image then how?Thanks for your answer.Cheju
Hi Satish, Thanks! Could you please share the source code. So it will be helpful to understand and implement!Cheju
I've updated my answer again. Please let me know, if you need specific info.Laux
Please help! As I asked before! Thanks in advance!Cheju
are you clear with creating GMSMapView and GMS markers?Laux
Yes! Each and every 3 seconds I am calling an API. Now I want to move and rotate the marker.but its jumping. Please help me in swift.Cheju
I've worked in many ways with GMSMarker but it didn't worked for me in case of rotation. So, i took an ImageView and its working fine.Laux
Great! Can you please share the codebase.Swift is preferred most!Cheju
Only that particular Viewcontroller. If yes, I will share my mail id!Cheju
sure, but I've worked in Objective-c. Don't worry, there might not be much changes to do to work in swift.Laux
saved my day brother.. I needed to get the direction for arrow heads.. very helpfull. Thanks a lot +1Hel
@AhsanEbrahim.. I did it in very easy and simple way.Cheju
@Captain how did you do it ?Hel
@Captain how did you do it ?Ambuscade
@Captain : can you share the code snippet in swift3?Railway
Hello can you share the code for Objective-C for same issue to rorate the GMSMarker Pls? Thanks a lot.Leannaleanne
hi @SatishA...can u share the code base of view controller here?Mithridate

© 2022 - 2024 — McMap. All rights reserved.