Rotate image as per the direction of user follows in Map - iphone

I want to rotate the bike / car image in the direction of user follows. Right now the image follows the direction but looks non directive, we can say the point is not follows the direction :
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"Bike.png"];
annotationView.annotation = annotation;
return annotationView;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy < 0)
return;
if (!newLocation)
return;
currentLocation = newLocation;
if(currentLocation != nil)
{
if (myAnnotation)
{
[self.myMapView removeAnnotation:myAnnotation];
}
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
myAnnotation = [[MyAnnotation alloc] initWithCoordinates:location title:#"Current Location" subTitle:nil];
[self.myMapView addAnnotation:myAnnotation];
}
}

Check out Core Location's -[CLLocationManagerDelegate locationManager:didUpdateHeading:].

Related

removeAnnotation method not called

Annotation method removeAnnotation is not called, so that the images on map duplicated while update location. How to call removeAnnotation method so that we can remove image from old location.
<pre>
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"Bike.png"];
annotationView.annotation = annotation;
return annotationView;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// here do
//UIImage *pinImage = [UIImage imageNamed:#"Bike.png"];
CLLocation *currentLocation = newLocation;
if(currentLocation != nil)
{
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinates:location title:#"India" subTitle:#"Sarvopari Mall"];
if (newLocation)
{
[self.myMapView removeAnnotation:annotation];
}
[self.myMapView addAnnotation:annotation];
}
}
</pre>
You should keep the annotation as an instance variable or property while adding it to map. Then while location updates, remove the previous annotation(stored in instance variable) and add new.
You should modify your UIViewController like this:
#interface YourViewController : UIViewController
{
MyAnnotation *annotation;
}
#end
And modify your LocationManager Delegate methods like this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
// here do
//UIImage *pinImage = [UIImage imageNamed:#"Bike.png"];
CLLocation *currentLocation = newLocation;
if(currentLocation != nil)
{
//Removes previous annotation
if(annotation){
[self.myMapView removeAnnonation:annotation];
}
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
//Keeps annotation in an instance variable
annotation = [[MyAnnotation alloc] initWithCoordinates:location title:#"India" subTitle:#"Sarvopari Mall"];
[self.myMapView addAnnotation:annotation];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
[self.myMapView removeAnnotation:annotation];
annotationView.image = [UIImage imageNamed:#"Bike.png"];
annotationView.annotation = annotation;
return annotationView;
}
From looking at the code above, probably [self.myMapView removeAnnotation:annotation]; will be called because if (newLocation) will be true as you are updating to a new location.
Otherwise, in the code below you are removing and adding exactly the same annotation as you can see.
if (newLocation)
{
[self.myMapView removeAnnotation:annotation];
}
[self.myMapView addAnnotation:annotation];
I think you should instead create another annotation with the oldLocation and remove that one instead.

Rotate image in the direction of which user moving through compass methods of class CLLocationManagerDelegate

I want to move the bike / car in the direction of which, user moving through compass methods. For, that i have used didUpdateHeading method, but still i can't able to move the bike image in the user's direction.
<pre>
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"Bike.png"];
return annotationView;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
currentLocation = newLocation;
previousLocation = oldLocation;
if(currentLocation != nil)
{
if (myAnnotation)
{
[self.myMapView removeAnnotation:myAnnotation];
}
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
myAnnotation = [[MyAnnotation alloc] initWithCoordinates:location title:#"Current Location" subTitle:nil];
[self.myMapView addAnnotation:myAnnotation];
}
}
- (void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading {
if (newHeading.headingAccuracy > 0) {
float heading = -1.0f * M_PI * newHeading.magneticHeading / 180.0f;
annotationView.transform = CGAffineTransformMakeRotation(heading);
}
}
</pre>
- (void)viewDidLoad
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
locationManager.headingFilter = kCLHeadingFilterNone;
[locationManager startUpdatingHeading];
[self.view bringSubviewToFront:_compass_image];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
double rotation = newHeading.magneticHeading * 3.14159 / 180;
[_compass_image setTransform:CGAffineTransformMakeRotation(-rotation)];
}

Algorithm for the direction in which user moves

I want to show custom car / bike image in the direction of which user moving, right now the problem is that, bike goes in the direction of which user moves, but when user come reverse it bike also shown in reverse mode.
Below is the code :
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"Bike.png"];
annotationView.transform = CGAffineTransformMakeRotation(45);
return annotationView;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy < 0)
return;
if (!newLocation)
return;
currentLocation = newLocation;
previousLocation = oldLocation;
if(currentLocation != nil)
{
if (myAnnotation)
{
[self.myMapView removeAnnotation:myAnnotation];
}
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
myAnnotation = [[MyAnnotation alloc] initWithCoordinates:location title:#"Current Location" subTitle:nil];
[self.myMapView addAnnotation:myAnnotation];
}
}

Want to show custom images on the map with user update location

I want to show bike/car image on google maps with rotation that user follows.
How it can be possible. I have added didUpdateToLocation method to update the location of user and added viewForAnnotation to display the current location of the bike. But not able to show the bike on the map which user follows.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if(currentLocation != nil)
{
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinates:location title:#"India" subTitle:#"Sarvopari Mall"];
[self.myMapView addAnnotation:annotation];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation{
MKAnnotationView *result = nil;
if ([annotation isKindOfClass:[MyAnnotation class]] == NO)
{
return result;
}
if ([mapView isEqual:self.myMapView] == NO)
{
return result;
}
MyAnnotation *senderAnnotation = (MyAnnotation *)annotation;
NSString *pinReusableIdentifier =
[MyAnnotation
reusableIdentifierforPinColor:senderAnnotation.pinColor];
MKAnnotationView *annotationView = (MKAnnotationView *)
[mapView
dequeueReusableAnnotationViewWithIdentifier:
pinReusableIdentifier];
if (annotationView == nil){
annotationView =
[[MKAnnotationView alloc] initWithAnnotation:senderAnnotation
reuseIdentifier:pinReusableIdentifier];
annotationView.canShowCallout = YES;
}
UIImage *pinImage = [UIImage imageNamed:#"Bike.png"];
if (pinImage != nil){
annotationView.image = pinImage;
annotationView.canShowCallout = YES;
}
result = annotationView;
return result;
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"Bike.png"];
annotationView.annotation = annotation;
return annotationView;
}

MapKit User Location (Blue Dot & Circle) will not show

I cannot figure out why the current location circle will not appear. I have custom annotations that appear just fine... and the map is taken to the current location of the user... but, the circle is not appearing.
Here is my code. Thanks in advance!
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
[mapView setMapType:MKMapTypeStandard];
mapView.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.span.longitudeDelta = 0.005;
region.span.latitudeDelta = 0.005;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D loc = [newLocation coordinate];
[mapView setCenterCoordinate:loc];
}
-(MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.canShowCallout = YES;
if ([annotationView.annotation.title isEqualToString:#"One"]) {
UIImage *pinImage = [UIImage imageNamed:#"one.png"];
[annotationView setImage:pinImage];
}
if ([annotationView.annotation.title isEqualToString:#"Two"]) {
UIImage *pinImage = [UIImage imageNamed:#"two.png"];
[annotationView setImage:pinImage];
}
annotationView.annotation = annotation;
return annotationView;
}
Add this to the top of the viewForAnnotation method:
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil; //return nil to use default blue dot view