Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I show compass/heading on mapkit mapview

on the iPhone 3GS in the "Maps" app you can click the icon which usually shows your position twice and the blue dot gains what looks like a beam from a headlamp, basically showing you the direction you are facing on the map and rotating the image accordingly.

Is this option available using MapKit MapView ?

I'm know that I can get my heading with something like

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

// If the accuracy is valid, process the event.

if (newHeading.headingAccuracy > 0) {

    CLLocationDirection theHeading = newHeading.magneticHeading;
    ...
}

}

but I don't know how to get that nice headlamp effect in Mapkit and there doesn't seem to be any documentation.

Any ideas?

like image 240
Erik Carlson Avatar asked Jun 12 '10 21:06

Erik Carlson


2 Answers

Adding user tracking mode also helps. I know I am late, but possibly a help to other developers like me :)

self.mapView.userTrackingMode = RMUserTrackingModeFollowWithHeading;
like image 156
ScarletWitch Avatar answered Sep 20 '22 16:09

ScarletWitch


I found a solution:

I rotate the map using the available heading-information with

[mapView setTransform:CGAffineTransformMakeRotation(heading.magneticHeading * M_PI / -180.0)];

Therefore the "beam" always points to the top of the device. I now just display an ImageView on top of the map and change it's position in locationManager:didUpdateToLocation:fromLocation:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        // scroll to new location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
    [self.mapView setRegion:region animated:YES];

        // set position of "beam" to position of blue dot
    self.headingAngleView.center = [self.mapView convertCoordinate:newLocation.coordinate toPointToView:self.view];
        // slightly adjust position of beam
    self.headingAngleView.frameTop -= self.headingAngleView.frameHeight/2 + 8;
 }

Whereby frameTop and frameHeight are shortcuts for frame.origin.y and frame.size.height. It is not ideal and sometimes lacks a little bit when the dot changes it's position, but I'm happy with the solution b/c it works.

Have a look at my OpenSource framework MTLocation which does this all (and a lot of other cool Map-related stuff for you):

MTLocation

like image 35
myell0w Avatar answered Sep 22 '22 16:09

myell0w