Reputation: 11
In my app i need to show user moving along with the direction he is moving in a GMSMapView, So I have putted custom GMSMarker and set the image(Ex. Bike or Car) and animating that marker when user starts moving and changing the angle of the marker in locationManager didUpdateHeading delegate method, Because GMSMarker image ( Bike or Car) should start heading towards user moving direction.
Below is the code am using, But its working when user moves slowly say walking and not working properly when the user moving fast say in bike or car with 40+ speed.
- (void)viewDidLoad {
[super viewDidLoad];
if(locationManager == nil) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 10.0;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
[locationManager requestAlwaysAuthorization];
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = 5;
[locationManager startUpdatingHeading];
}
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
// Use the true heading if it is valid.
CLLocationDirection direction = newHeading.magneticHeading;
CGFloat radians = -direction / 180.0 * M_PI;
//For Rotate Niddle
CGFloat angle = RADIANS_TO_DEGREES(radians);
[self rotateArrowView:angle];
}
-(void)rotateArrowView:(CGFloat)degrees {
currentLocationMarker.rotation = degrees;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
currentLocation = [locations lastObject];
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
currentLocationMarker.position = currentLocation.coordinate;
[CATransaction commit];
}
Can anyone tell me what I should do now to show proper exact heading when the user moves fast.
Upvotes: 1
Views: 1670
Reputation: 11
Well, I am not really sure, but it seems that heading is not very accurate while driving fast. I see several options:
A = oldUserLocation (Vector 2D)
B = newUserLocation (Vector 2D)
DeltaMovement = B - A
Then, assuming that north direction can be represented as 2D Vector V(0,1), you can use math functions (I prefer https://github.com/nicklockwood/VectorMath for that, but I'm sure there are good obj-c stuff for that as well) to get angle between movement vector and north.
The drawback is that you get drift when taking turns. You can always of course use more than one old location - that allows you to make it even more "noise insensitive".
Upvotes: 1