Reputation: 3044
I have had a look around but have been able to find very little on the subject. I am trying to implement a "find your car" feature into an app I am creating.
I have a map view that the user can use to store the location of their car. They can then click "find car" to displays their current location and the car on the mapView. (see below)
My problem: How can I get the map to rotate to show which way the user is facing? So I can show the user which way they need to walk. Much like the native maps ap.
From the reading I have done I believe that this will be done in the didUpdateToLocation: delegate method but am not too sure.
(just realise my user is in a lake but thats not the problem)
This is the code that I have used to add the pin and fit them of on the screen.
OLD CODE
EDIT:
I have sort of manages to get it working using the link provided by fguchelaar in the comments. I have change my code to look like this. This code does allow the map to rotate but has some strange behaviour. See HERE
- (IBAction)BTNPinCar:(id)sender {
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
float longitude=location.coordinate.longitude;
float latitude=location.coordinate.latitude;
NSLog(@"dLongitude : %f", longitude);
NSLog(@"dLatitude : %f", latitude);
[self pins:latitude lon:longitude];
parked.latitude = latitude;
parked.longitude = longitude;
//set the reigion to the coords and a mile distance
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(parked, startZoom , startZoom );
//change the region and animate it
[MapView setRegion:viewRegion animated:YES];
}
- (IBAction)BTNFindCar:(id)sender {
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in MapView.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[MapView setVisibleMapRect:zoomRect animated:YES];\
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = 5;
[locationManager startUpdatingHeading];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
// [MapView setTransform:CGAffineTransformMakeRotation(-1 * newHeading.magneticHeading * M_PI / 180)];
double rotation = newHeading.magneticHeading * 3.14159 / 180;
CGPoint anchorPoint = CGPointMake(0, -23); // The anchor point for your pin
[MapView setTransform:CGAffineTransformMakeRotation(-rotation)];
[[MapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKAnnotationView * view = [MapView viewForAnnotation:obj];
[view setTransform:CGAffineTransformMakeRotation(rotation)];
[view setCenterOffset:CGPointApplyAffineTransform(anchorPoint, CGAffineTransformMakeRotation(rotation))];
}];
}
EDIT 2:
Solved the rotation issue;
changed
[MapView setTransform:CGAffineTransformMakeRotation(-rotation)];
for
[MapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
Upvotes: 1
Views: 3022
Reputation: 3044
The answer to get the map to rotate is as follows.
First start the location manager and set the delegate.
// locationManager update as location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
// Start location services to get the true heading.
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
The above code starts tracking the users location. Then you need to start tacking the users heading to rotate the map
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = 5;
[locationManager startUpdatingHeading];
}
After that implement the LocationManager delegate method.
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading;
Finally set the tracking mode in that delegate method.
[MapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
Hope this helps anyone with the same problem
Upvotes: 4