MapView supports delegate for move, zoom and doubleTap-Events
Showing
4 changed files
with
84 additions
and
30 deletions
@@ -11,6 +11,7 @@ | @@ -11,6 +11,7 @@ | ||
11 | 11 | ||
12 | #import "RMFoundation.h" | 12 | #import "RMFoundation.h" |
13 | #import "RMLatLong.h" | 13 | #import "RMLatLong.h" |
14 | +#import "RMMapViewDelegate.h" | ||
14 | 15 | ||
15 | // iPhone-specific mapview stuff. | 16 | // iPhone-specific mapview stuff. |
16 | // Handles event handling, whatnot. | 17 | // Handles event handling, whatnot. |
@@ -30,9 +31,10 @@ typedef struct { | @@ -30,9 +31,10 @@ typedef struct { | ||
30 | @interface RMMapView : UIView | 31 | @interface RMMapView : UIView |
31 | { | 32 | { |
32 | RMMapContents *contents; | 33 | RMMapContents *contents; |
34 | + id<RMMapViewDelegate> delegate; | ||
33 | 35 | ||
34 | - bool enableDragging; | ||
35 | - bool enableZoom; | 36 | + BOOL enableDragging; |
37 | + BOOL enableZoom; | ||
36 | RMGestureDetails lastGesture; | 38 | RMGestureDetails lastGesture; |
37 | } | 39 | } |
38 | 40 | ||
@@ -40,6 +42,10 @@ typedef struct { | @@ -40,6 +42,10 @@ typedef struct { | ||
40 | // property. The contents structure holds the actual map bits. | 42 | // property. The contents structure holds the actual map bits. |
41 | @property (readonly) RMMapContents *contents; | 43 | @property (readonly) RMMapContents *contents; |
42 | 44 | ||
45 | +// do not retain the delegate so you can let the corresponding controller implement the | ||
46 | +// delegate without circular references | ||
47 | +@property (assign) id<RMMapViewDelegate> delegate; | ||
48 | + | ||
43 | - (void)moveToLatLong: (CLLocationCoordinate2D)latlong; | 49 | - (void)moveToLatLong: (CLLocationCoordinate2D)latlong; |
44 | - (void)moveToXYPoint: (RMXYPoint)aPoint; | 50 | - (void)moveToXYPoint: (RMXYPoint)aPoint; |
45 | 51 |
@@ -8,12 +8,21 @@ | @@ -8,12 +8,21 @@ | ||
8 | 8 | ||
9 | #import "RMMapView.h" | 9 | #import "RMMapView.h" |
10 | #import "RMMapContents.h" | 10 | #import "RMMapContents.h" |
11 | +#import "RMMapViewDelegate.h" | ||
11 | 12 | ||
12 | #import "RMTileLoader.h" | 13 | #import "RMTileLoader.h" |
13 | 14 | ||
14 | #import "RMMercatorToScreenProjection.h" | 15 | #import "RMMercatorToScreenProjection.h" |
15 | #import "RMMarker.h" | 16 | #import "RMMarker.h" |
16 | 17 | ||
18 | +@implementation RMMapView (Internal) | ||
19 | + BOOL delegateHasBeforeMapMove; | ||
20 | + BOOL delegateHasAfterMapMove; | ||
21 | + BOOL delegateHasBeforeMapZoomByFactor; | ||
22 | + BOOL delegateHasAfterMapZoomByFactor; | ||
23 | + BOOL delegateHasDoubleTapOnMap; | ||
24 | +@end | ||
25 | + | ||
17 | @implementation RMMapView | 26 | @implementation RMMapView |
18 | 27 | ||
19 | -(void) initValues | 28 | -(void) initValues |
@@ -69,28 +78,58 @@ | @@ -69,28 +78,58 @@ | ||
69 | return [[contents retain] autorelease]; | 78 | return [[contents retain] autorelease]; |
70 | } | 79 | } |
71 | 80 | ||
81 | +#pragma mark Delegate | ||
82 | + | ||
83 | +@dynamic delegate; | ||
84 | + | ||
85 | +- (void) setDelegate: (id<RMMapViewDelegate>) _delegate | ||
86 | +{ | ||
87 | + if (delegate == _delegate) return; | ||
88 | + delegate = _delegate; | ||
89 | + | ||
90 | + delegateHasBeforeMapMove = [(NSObject*) delegate respondsToSelector: @selector(beforeMapMove:)]; | ||
91 | + delegateHasAfterMapMove = [(NSObject*) delegate respondsToSelector: @selector(afterMapMove:)]; | ||
92 | + | ||
93 | + delegateHasBeforeMapZoomByFactor = [(NSObject*) delegate respondsToSelector: @selector(beforeMapZoom: byFactor: near:)]; | ||
94 | + delegateHasAfterMapZoomByFactor = [(NSObject*) delegate respondsToSelector: @selector(afterMapZoom: byFactor: near:)]; | ||
95 | + | ||
96 | + delegateHasDoubleTapOnMap = [(NSObject*) delegate respondsToSelector: @selector(doubleTapOnMap:)]; | ||
97 | + | ||
98 | + NSLog(@"%d %d %d %d %d", delegateHasBeforeMapMove, delegateHasAfterMapMove, delegateHasBeforeMapZoomByFactor, delegateHasAfterMapZoomByFactor, delegateHasDoubleTapOnMap); | ||
99 | + | ||
100 | +} | ||
101 | + | ||
102 | +- (id<RMMapViewDelegate>) delegate | ||
103 | +{ | ||
104 | + return delegate; | ||
105 | +} | ||
106 | + | ||
72 | #pragma mark Movement | 107 | #pragma mark Movement |
73 | 108 | ||
74 | -(void) moveToXYPoint: (RMXYPoint) aPoint | 109 | -(void) moveToXYPoint: (RMXYPoint) aPoint |
75 | { | 110 | { |
76 | - // TODO Add delegate hooks | 111 | + if (delegateHasBeforeMapMove) [delegate beforeMapMove: self]; |
77 | [contents moveToXYPoint:aPoint]; | 112 | [contents moveToXYPoint:aPoint]; |
113 | + if (delegateHasAfterMapMove) [delegate afterMapMove: self]; | ||
78 | } | 114 | } |
79 | -(void) moveToLatLong: (CLLocationCoordinate2D) point | 115 | -(void) moveToLatLong: (CLLocationCoordinate2D) point |
80 | { | 116 | { |
81 | - // TODO Add delegate hooks | 117 | + if (delegateHasBeforeMapMove) [delegate beforeMapMove: self]; |
82 | [contents moveToLatLong:point]; | 118 | [contents moveToLatLong:point]; |
119 | + if (delegateHasAfterMapMove) [delegate afterMapMove: self]; | ||
83 | } | 120 | } |
84 | 121 | ||
85 | - (void)moveBy: (CGSize) delta | 122 | - (void)moveBy: (CGSize) delta |
86 | { | 123 | { |
87 | - // TODO Add delegate hooks | 124 | + if (delegateHasBeforeMapMove) [delegate beforeMapMove: self]; |
88 | [contents moveBy:delta]; | 125 | [contents moveBy:delta]; |
126 | + if (delegateHasAfterMapMove) [delegate afterMapMove: self]; | ||
89 | } | 127 | } |
90 | - (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center | 128 | - (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center |
91 | { | 129 | { |
92 | - // TODO Add delegate hooks | 130 | + if (delegateHasBeforeMapZoomByFactor) [delegate beforeMapZoom: self byFactor: zoomFactor near: center]; |
93 | [contents zoomByFactor:zoomFactor near:center]; | 131 | [contents zoomByFactor:zoomFactor near:center]; |
132 | + if (delegateHasAfterMapZoomByFactor) [delegate afterMapZoom: self byFactor: zoomFactor near: center]; | ||
94 | } | 133 | } |
95 | 134 | ||
96 | #pragma mark Event handling | 135 | #pragma mark Event handling |
@@ -212,30 +251,12 @@ | @@ -212,30 +251,12 @@ | ||
212 | //Double-tap detection (currently used for debugging pixelToLatLng() method) | 251 | //Double-tap detection (currently used for debugging pixelToLatLng() method) |
213 | if (touch.tapCount == 2) | 252 | if (touch.tapCount == 2) |
214 | { | 253 | { |
215 | -/* NSLog(@"***************************************************"); | ||
216 | - NSLog(@"Begin double-tap pixel/LatLng translation debug test"); | ||
217 | - CGPoint pixel = [touch locationInView:self]; | ||
218 | - NSLog(@"Double-tap detected at: x=%f, y=%f", pixel.x, pixel.y); | ||
219 | - CLLocationCoordinate2D touchLatLng = [self pixelToLatLong:pixel]; | ||
220 | - | ||
221 | - NSLog(@"Double-tap (x=%f, y=%f) is equivalent to: %f, %f", pixel.x, pixel.y, touchLatLng.latitude, touchLatLng.longitude); | ||
222 | - | ||
223 | - RMXYPoint merc = [[contents mercatorToScreenProjection] projectScreenPointToXY:pixel]; | ||
224 | - NSLog(@"Which is mercator %f %f", merc.x, merc.y); | ||
225 | - | ||
226 | - CLLocationCoordinate2D point; | ||
227 | - point.latitude = touchLatLng.latitude; | ||
228 | - point.longitude = touchLatLng.longitude; | ||
229 | - CGPoint screenPoint = [self latLongToPixel:point]; | ||
230 | - | ||
231 | - NSLog(@"Converted LatLng to Pixel says we tapped at: x=%f, y=%f", screenPoint.x, screenPoint.y); | ||
232 | - NSLog(@"***************************************************"); | ||
233 | -*/ | ||
234 | - | ||
235 | - | ||
236 | - // For consistancy with the built-in map app, I want to do something like this: | ||
237 | - // [contents zoomInToNextNativeZoom]; | ||
238 | - | 254 | + if (delegateHasDoubleTapOnMap) { |
255 | + [delegate doubleTapOnMap: self]; | ||
256 | + } else { | ||
257 | + // TODO: default behaviour | ||
258 | + // [contents zoomInToNextNativeZoom]; | ||
259 | + } | ||
239 | } | 260 | } |
240 | //*************************************************************************************** | 261 | //*************************************************************************************** |
241 | 262 |
MapView/Map/RMMapViewDelegate.h
0 → 100644
1 | +// | ||
2 | +// RMMapViewDelegate.h | ||
3 | +// MapView | ||
4 | +// | ||
5 | +// Created by Hauke Brandes on 31.10.08. | ||
6 | +// Copyright 2008 Orbster GmbH. All rights reserved. | ||
7 | +// | ||
8 | + | ||
9 | +#import <UIKit/UIKit.h> | ||
10 | + | ||
11 | +@class RMMapView; | ||
12 | + | ||
13 | +@protocol RMMapViewDelegate | ||
14 | + | ||
15 | +@optional | ||
16 | + | ||
17 | +- (void) beforeMapMove: (RMMapView*) map; | ||
18 | +- (void) afterMapMove: (RMMapView*) map ; | ||
19 | + | ||
20 | +- (void) beforeMapZoom: (RMMapView*) map byFactor: (float) zoomFactor near:(CGPoint) center; | ||
21 | +- (void) afterMapZoom: (RMMapView*) map byFactor: (float) zoomFactor near:(CGPoint) center; | ||
22 | + | ||
23 | +- (void) doubleTapOnMap: (RMMapView*) map; | ||
24 | + | ||
25 | +@end |
@@ -140,6 +140,7 @@ | @@ -140,6 +140,7 @@ | ||
140 | 1296F5600EB8743500FF25E0 /* RMMarkerStyle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMMarkerStyle.m; sourceTree = "<group>"; }; | 140 | 1296F5600EB8743500FF25E0 /* RMMarkerStyle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMMarkerStyle.m; sourceTree = "<group>"; }; |
141 | 1296F5620EB8745300FF25E0 /* RMMarkerStyles.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMMarkerStyles.h; sourceTree = "<group>"; }; | 141 | 1296F5620EB8745300FF25E0 /* RMMarkerStyles.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMMarkerStyles.h; sourceTree = "<group>"; }; |
142 | 1296F5630EB8745300FF25E0 /* RMMarkerStyles.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMMarkerStyles.m; sourceTree = "<group>"; }; | 142 | 1296F5630EB8745300FF25E0 /* RMMarkerStyles.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMMarkerStyles.m; sourceTree = "<group>"; }; |
143 | + 12F2031E0EBB65E9003D7B6B /* RMMapViewDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMMapViewDelegate.h; sourceTree = "<group>"; }; | ||
143 | 1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; | 144 | 1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; |
144 | 1D3623240D0F684500981E51 /* MapViewAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MapViewAppDelegate.h; sourceTree = "<group>"; }; | 145 | 1D3623240D0F684500981E51 /* MapViewAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MapViewAppDelegate.h; sourceTree = "<group>"; }; |
145 | 1D3623250D0F684500981E51 /* MapViewAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MapViewAppDelegate.m; sourceTree = "<group>"; }; | 146 | 1D3623250D0F684500981E51 /* MapViewAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MapViewAppDelegate.m; sourceTree = "<group>"; }; |
@@ -521,6 +522,7 @@ | @@ -521,6 +522,7 @@ | ||
521 | B8C9746A0E8A1A50007D16AD /* RMMapView.m */, | 522 | B8C9746A0E8A1A50007D16AD /* RMMapView.m */, |
522 | B8C974B60E8A280A007D16AD /* RMMapContents.h */, | 523 | B8C974B60E8A280A007D16AD /* RMMapContents.h */, |
523 | B8C974B70E8A280A007D16AD /* RMMapContents.m */, | 524 | B8C974B70E8A280A007D16AD /* RMMapContents.m */, |
525 | + 12F2031E0EBB65E9003D7B6B /* RMMapViewDelegate.h */, | ||
524 | B8C974B30E8A23C5007D16AD /* Coordinate Systems */, | 526 | B8C974B30E8A23C5007D16AD /* Coordinate Systems */, |
525 | B83E64E20E80E73F001663B6 /* Projections */, | 527 | B83E64E20E80E73F001663B6 /* Projections */, |
526 | B83E64EB0E80E73F001663B6 /* Tile Source */, | 528 | B83E64EB0E80E73F001663B6 /* Tile Source */, |
-
Please register or login to post a comment