4

I am programming a map app on iPhone and want the map to rotate as the user changes his direction. I have read most of the posts on stackoverflow. Most of them suggest the use of setUserTrackingMode with MKUserTrackingModeFollowWithHeading if we are working with iOS 5 or later. This does not seem to work with me for some reason. Following is my code:

-(IBAction)getLocation  //This is a button
{
    mapView.showsUserLocation=YES;  //mapView is the instance of MKMapView
    [mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
}

This only shows the user location but if I move the phone, it doesn't rotate. One more thing is, I downloaded a project from internet, and I included this line. It worked there only for the first time. I have no idea why this is happening.

Any suggestions?

Autonomous
  • 8,935
  • 1
  • 38
  • 77

3 Answers3

5

You need to wait for the 'MapView' finish loading...

follow:

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
    mapView.userTrackingMode = MKUserTrackingModeFollow;
}

follow & heading:

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
    mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
}
Ofir Malachi
  • 1,145
  • 14
  • 20
2

The easier way to do this is to include an MKUserTrackingBarButtonItem instead of creating your own button. It acts exactly the same as the button in the iOS 5 Maps app and is easy to set up.

Here's how to use it:

// You should have an outlet to your map view called mapView
MKUserTrackingBarButtonItem *userTrackingButton;
userTrackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView];

// You need an outlet to your toolbar too
[self.toolbar setItems:[NSArray arrayWithObject:userTrackingButton]];
nevan king
  • 112,709
  • 45
  • 203
  • 241
  • I added a toolbar in the .xib file and then in the `ViewController.h`, I have: `-(IBAction)track:(id)sender;` Now, in the `ViewController.m` file, in `viewDidLoad`: `MKUserTrackingBarButtonItem *myButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView]; [myButton setTarget:self]; [myButton setAction:@selector(track:)]; self.toolbarItems = [NSArray arrayWithObjects:myButton, nil]; [myButton release];` and then somewhere else I defined `track` and added `showsUserLocation` and `setUserTrackingMode`. I am simulating the location too, still map doesn't rotate. – Autonomous Aug 16 '12 at 23:57
  • 1
    That button takes care of target and action. All you need to do is give it your map view and it should work. – nevan king Aug 17 '12 at 07:43
  • Sorry, I am new in this, Can you please elaborate... so do I just write `MKUserTrackingBarButtonItem *trackButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];`? By doing this I don't see it getting added in the final view (I have a single view application), or do I put a toolbar in the .xib file and then link it? – Autonomous Aug 17 '12 at 07:57
  • 1
    You'll need to put it in the toolbar to for it to appear. I've updated my answer with a little code. Make sure you have the outlets to a map and toolbar using the same names as I used. – nevan king Aug 17 '12 at 09:22
-2

Change "Animated" to "animated" and try again

Craig
  • 8,093
  • 8
  • 42
  • 74
  • Sorry for the typo... it still doesn't work, but program runs fine. – Autonomous Aug 14 '12 at 01:24
  • Is there some way you could confirm the userTrackingMode has really been set? If there is a loop or an action you can trigger by hand, add some debug text that prints out the trackingMode. If that really is set (and your device does actually have a compass) then there is no reason it shouldn't rotate like other map apps. – Craig Aug 14 '12 at 07:48
  • I searched as per your suggestions and I found a similar issue `[here](http://stackoverflow.com/questions/7941763/setting-mkusertrackingmodefollowwithheading)`. There, they set the value 2 for `MKUserFollowWithHeading' and then print it. Can anybody explain how to do that? Also, they report some erroneous behaviour. It seems to be solved by setting `mapview's` delegate as `self` – Autonomous Aug 14 '12 at 09:54
  • can somebody please explain me how to print the value of `setUserTrackingMode` mentioned above in this comment? – Autonomous Aug 14 '12 at 10:08
  • If ([mapview userTrackingMode] == mkusertrackingmodeFollow) NSLog(@"follow"); – Craig Aug 14 '12 at 17:34
  • Ok, I tested out the above command and I can see `follow` getting printed onto the console. I tried setting mapView's delegate as self, but still it didn't work. I am running this app on simulator, by any chance, is that the reason of this behavior? I don't think simulator plays any role here because I have seen it working on simulator. Any other ideas? – Autonomous Aug 14 '12 at 23:21
  • Also, when I set `mapView's` delegate as `self`, I get a warning: `Assigning to id from incompatible type 'FirstMapViewController *'` what does this mean? – Autonomous Aug 14 '12 at 23:23
  • 1
    Yes if you are using this on a simulator you won't get a heading, unless your iOS device is plugged in and has a compass. Your mac doesn't have a compass does it? As for the delegate problem you should ask a new question. The answer will be to mark your MapViewController as implementing the MapViewDelegate protocol. – Craig Aug 15 '12 at 09:28