Ronak Ronak - 5 months ago 29
Swift Question

How to integrate google map navigation in iOS to our app?

In my app to integrate google map navigation for left right direction. So how it's possible

Thanks

Answer

//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];
Comments