4

I'm trying to change the User Annotation in my app so that it shows the usual blue dot, but with a triangle coming off of it to show which direction the user is facing (I'd rather rotate the user annotation than the entire map, which is what MKUserTrackingModeFollowWithHeading does). I've got a rudimentary version working, but it has some weird behavior.

First, some code:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
     if ([annotation isKindOfClass:[MKUserLocation class]]) {
          _userLocationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"userLocationIdentifier"];
          //use a custom image for the user annotation
          _userLocationView.image = [UIImage imageNamed:@"userLocationCompass.png"];

          return _userLocationView;

     } else {
          return nil;
     }

}

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

     //rotate user location annotation based on heading from location manager.
     if (!_locatorButton.hidden) {
          CLLocationDirection direction = newHeading.magneticHeading;

          CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadians(direction));
          _userLocationView.transform = transform;
     }

}

-(void)GPSButtonPressed:(id)sender {
    if (self.GPSEnabled) {
        //if GPS is already on, disable it.
        _mapview.showsUserLocation = NO;
        [_mapview removeAnnotation:_mapview.userLocation];
        self.GPSEnabled = NO;

        [_locationManager stopUpdatingHeading];

    } else {
        //enable GPS.
        _mapview.showsUserLocation = YES;
        self.GPSEnabled = YES;

        if ([CLLocationManager headingAvailable]) {
            [_locationManager startUpdatingHeading];
        }


    }
}

The user location annotation image shows up fine, and rotates based on the heading, but here are the funny behaviors:

1- The annotation does not follow my location-- it only stays in one place, but rotates with the correct heading. If I turn off the GPS with the "GPS Button", then turn it back on, the annotation shows up in the correct place, but still won't follow as I walk.

2- If I scroll the map, the annotation pops back to due north, then quickly rotates to the correct heading, causing an annoying flickering effect.

3- If I turn off the GPS and remove the user location, the annotation is removed as intended, but if I then scroll the map, the annotation pops back to the screen rather than staying hidden.

What am I doing wrong? Thanks for any helpful hints!

RL2000
  • 913
  • 10
  • 20
  • 1
    Regarding behavior 1, see http://stackoverflow.com/questions/11432746/custom-annotation-view-for-userlocation-not-moving-the-mapview. Re behavior 3: In `GPSButtonPressed`, I don't recommend calling removeAnnotation on the map view's userLocation (simply set showsUserLocation to NO). Also see http://stackoverflow.com/questions/8555903/erratic-behavior-of-showsuserlocation-in-mkmapview. –  Jan 04 '13 at 14:27
  • Thanks, Anna Karenina! That was a huge help-- I'm using CLLocationManager instead of the MapView's showsUserLocation, so the annotation now moves with me and rotates correctly. Behavior number 2 is still there, but that's still better than earlier this morning :) – RL2000 Jan 04 '13 at 15:43
  • Did you ever find a solution for issue number 2? I'm doing something similar with custom userLocation icon and rotating the map manually rather than using followWithHeading. Similar to this: http://stackoverflow.com/questions/7036246/how-to-rotate-mkmapview-and-keep-the-annotation-and-the-view-dont-rotate It seems when the map region changes all of the annotations flicker to the original rotation until the next heading change comes from the location manager. I've tried caching the last rotation value and resetting it in regionWillChange and regionDidChange, but that didn't work. – Tim Stephenson Jan 31 '13 at 01:03
  • Yeah, still no luck so far. I put the problem on the back burner, though, and haven't looked at it much. A friend with a newer iPhone told me he didn't see the problem when I sent the app to him for testing, though. I wonder if that's something... – RL2000 Feb 01 '13 at 01:23
  • Oh, interesting. Thanks. – Tim Stephenson Feb 08 '13 at 21:49

1 Answers1

2

Even if it is likely that you already fixed your 2. problem I still want to share the solution, which helped me with a similar problem.

I came across same problem: my custom user location arrow rotation was always popping back to north when the user scrolled or panned the map. I found the fix here: https://stackoverflow.com/a/12753320/2030629

according to the author it is a but in ios6 and can be fixed by adding

if(is6orMore) {
        [annView setTransform:CGAffineTransformMakeRotation(.001)]; //iOS6 BUG WORKAROUND !!!!!!!
 }

to mapView:viewForAnnotation.

I also set the transform in - mapView:regionDidChangeAnimated: again. Hope this will make someone else as happy as it made me :)

Community
  • 1
  • 1
nanako
  • 161
  • 8