Note that there are some explanatory texts on larger screens.

plurals
  1. POTurning user location annotation with heading
    primarykey
    data
    text
    <p>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.</p> <p>First, some code:</p> <pre><code>-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id&lt;MKAnnotation&gt;)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]; } } } </code></pre> <p>The user location annotation image shows up fine, and rotates based on the heading, but here are the funny behaviors:</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>What am I doing wrong? Thanks for any helpful hints!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CORegarding 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.
      singulars
    2. COThanks, 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 :)
      singulars
    3. CODid 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.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload