Permalink
Browse files

Added methods to MapView-category to automatically display a headingA…

…ngle-View on top of the mapView, if heading is enabled
  • Loading branch information...
myell0w committed Mar 10, 2011
1 parent c3a8bba commit 8985148c38ab8611ff0d2cfaf28cf91090bc35e5
Showing with 51 additions and 4 deletions.
  1. +5 −0 MKMapView+MTLocation.h
  2. +33 −1 MKMapView+MTLocation.m
  3. +13 −3 MTLocationManager.m
View
@@ -21,6 +21,11 @@
- (void)addGoogleBadge;
- (void)addGoogleBadgeAtPoint:(CGPoint)topLeftOfGoogleBadge;
+- (void)addHeadingAngleView;
+- (void)showHeadingAngleView;
+- (void)hideHeadingAngleView;
+- (void)moveHeadingAngleViewToCoordinate:(CLLocationCoordinate2D)coordinate;
+
- (void)rotateToHeading:(CLHeading *)heading animated:(BOOL)animated;
- (void)resetHeadingRotationAnimated:(BOOL)animated;
View
@@ -13,16 +13,18 @@
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "MKMapView+MTLocation.h"
+#import <objc/runtime.h>
#define kDefaultGoogleBadgeOriginX 12
#define kDefaultGoogleBadgeOriginY 340
+static char headingAngleViewKey = 'h';
@implementation MKMapView (MTLocation)
////////////////////////////////////////////////////////////////////////
#pragma mark -
-#pragma mark Google Badge
+#pragma mark Adding Overlay Views
////////////////////////////////////////////////////////////////////////
- (void)addGoogleBadge {
@@ -37,6 +39,36 @@ - (void)addGoogleBadgeAtPoint:(CGPoint)topLeftOfGoogleBadge {
[self.superview addSubview:googleView];
}
+- (void)addHeadingAngleView {
+ UIImageView *headingAngleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HeadingAngleSmall.png"]] autorelease];
+ headingAngleView.hidden = YES;
+
+ // add to superview
+ [self.superview addSubview:headingAngleView];
+ // add as associated object to MapView
+ objc_setAssociatedObject(self, &headingAngleViewKey, headingAngleView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+}
+
+- (void)showHeadingAngleView {
+ id headingAngleView = objc_getAssociatedObject(self, &headingAngleViewKey);
+
+ [headingAngleView setHidden:NO];
+}
+
+- (void)hideHeadingAngleView {
+ id headingAngleView = objc_getAssociatedObject(self, &headingAngleViewKey);
+
+ [headingAngleView setHidden:YES];
+}
+
+- (void)moveHeadingAngleViewToCoordinate:(CLLocationCoordinate2D)coordinate {
+ CGPoint center = [self convertCoordinate:coordinate toPointToView:self.superview];
+ id headingAngleView = objc_getAssociatedObject(self, &headingAngleViewKey);
+
+ center.y -= [headingAngleView frame].size.height/2 + 8;
+ [headingAngleView setCenter:center];
+}
+
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Rotation (Heading information)
View
@@ -54,6 +54,7 @@ - (void)dealloc {
- (void)stopAllServices {
// Reset transform on map
[self.mapView resetHeadingRotationAnimated:YES];
+ [self.mapView hideHeadingAngleView];
// stop location-services
[self.locationManager stopUpdatingLocation];
@@ -83,6 +84,8 @@ - (void)setMapView:(MKMapView *)mapView {
tapInterceptor.touchesMovedCallback = ^(NSSet * touches, UIEvent * event) {
// Reset transform on map
[blockSelf.mapView resetHeadingRotationAnimated:YES];
+ // hide heading angle overlay
+ [blockSelf.mapView hideHeadingAngleView];
// stop location-services
[[MTLocationManager sharedInstance].locationManager stopUpdatingLocation];
@@ -107,6 +110,9 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLoca
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: manager, @"locationManager",
newLocation, @"newLocation",
oldLocation, @"oldLocation", nil];
+
+ // move heading angle overlay to new coordinate
+ [self.mapView moveHeadingAngleViewToCoordinate:newLocation.coordinate];
[[NSNotificationCenter defaultCenter] postNotificationName:kMTLocationManagerDidUpdateToLocationFromLocation object:self userInfo:userInfo];
}
@@ -122,10 +128,14 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: manager, @"locationManager",
newHeading, @"newHeading", nil];
- // rotate map according to heading
- [self.mapView rotateToHeading:newHeading animated:YES];
+ if (newHeading.headingAccuracy > 0) {
+ // show heading angle overlay
+ [self.mapView showHeadingAngleView];
+ // rotate map according to heading
+ [self.mapView rotateToHeading:newHeading animated:YES];
+ }
- [[NSNotificationCenter defaultCenter] postNotificationName:kMTLocationManagerDidUpdateHeading object:self userInfo:userInfo];
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMTLocationManagerDidUpdateHeading object:self userInfo:userInfo];
}
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {

0 comments on commit 8985148

Please sign in to comment.