I am developing some navigation tasks on google map. I have to move markers as vehicles moves with turns as uber does in their app. I have tried different solutions as offered on @SO but It is not working as I need.
I am getting angle with previous lat/long with current lat/long and animating mapwithBearing with that rotation
Here is code
[CATransaction begin];
[CATransaction setAnimationDuration:2.0];
NSDictionary *data = [[result objectForKey:@"returnData"] objectForKey:@"data"];
if (![data isEqual: [NSNull null]]) {
driverLocationCoordinate = CLLocationCoordinate2DMake([[data objectForKey:@"lat"] doubleValue], [[data objectForKey:@"lng"] doubleValue]);
driverMarker.position = driverLocationCoordinate;
GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:driverLocationCoordinate.latitude
longitude:driverLocationCoordinate.longitude
zoom:16];
mapHomeView.camera = camera;
if ([data objectForKey:@"preLat"] != [NSNull null] && [data objectForKey:@"preLng"] !=[NSNull null]){
if ([[data objectForKey:@"preLat"] floatValue] != 0.0f && [[data objectForKey:@"preLng"] floatValue] != 0.0f) {
NSLog(@"pre_lat = %f and pre_lng = %f", [[data objectForKey:@"preLat"] floatValue], [[data objectForKey:@"preLng"] floatValue]);
CLLocationCoordinate2D previousCoordinates = CLLocationCoordinate2DMake([[data objectForKey:@"preLat"] floatValue], [[data objectForKey:@"preLng"] floatValue]);
driverMarker.rotation = [self DegreeBearing:previousCoordinates locationB:driverMarker.position];
[mapHomeView animateToBearing:driverMarker.rotation];
}
}
[CATransaction commit];
I just taken degree code from another @SO post, It works when I'm on straight road but when car is still or turning, it gives flickering
Here is code of getting angle from another SO post.
-(double) DegreeBearing:(CLLocationCoordinate2D) A locationB: (CLLocationCoordinate2D)B{
double dlon = [self ToRad:(B.longitude - A.longitude) ];
double dPhi = log(tan([self ToRad:(B.latitude)] / 2 + M_PI / 4) / tan([self ToRad:(A.latitude)] / 2 + M_PI / 4));
if (fabs(dlon) > M_PI){
dlon = (dlon > 0) ? (dlon - 2*M_PI) : (2*M_PI + dlon);
}
return [self ToBearing:(atan2(dlon, dPhi))];
}
-(double) ToRad: (double)degrees{
return degrees*(M_PI/180);
}
-(double) ToBearing:(double)radians{
return [self ToDegrees:radians] + 360 % 360;
}
-(double) ToDegrees: (double)radians{
return radians * 180 / M_PI;
}
Can anyone help on this or propose any other solution?
Some how working with this code. You can try with this code with some logics change. But, it will work fine.
define these two in .h file
method for get bearing value from old and new coordinates
Swift 3.1
for github link: ARCarMovement