RMMapTiledLayerView.m
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// RMMapTiledLayerView.m
// MapView
//
// Created by Thomas Rasch on 17.08.11.
// Copyright (c) 2011 Alpstein. All rights reserved.
//
#import "RMMapTiledLayerView.h"
#import "RMMapView.h"
#import "RMTileSource.h"
@interface RMMapOverlayView ()
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer;
- (void)handleTwoFingerDoubleTap:(UIGestureRecognizer *)recognizer;
@end
@implementation RMMapTiledLayerView
@synthesize delegate;
+ + layerClass
{
return [CATiledLayer class];
}
- (CATiledLayer *)tiledLayer
{
return (CATiledLayer *)self.layer;
}
- (id)initWithFrame:(CGRect)frame mapView:(RMMapView *)aMapView
{
if (!(self = [super initWithFrame:frame]))
return nil;
mapView = [aMapView retain];
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
self.opaque = NO;
CATiledLayer *tiledLayer = [self tiledLayer];
tiledLayer.levelsOfDetail = [[mapView tileSource] maxZoom];
tiledLayer.levelsOfDetailBias = [[mapView tileSource] maxZoom];
UITapGestureRecognizer *doubleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)] autorelease];
doubleTapRecognizer.numberOfTapsRequired = 2;
UITapGestureRecognizer *singleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)] autorelease];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
UITapGestureRecognizer *twoFingerDoubleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerDoubleTap:)] autorelease];
twoFingerDoubleTapRecognizer.numberOfTapsRequired = 2;
twoFingerDoubleTapRecognizer.numberOfTouchesRequired = 2;
UITapGestureRecognizer *twoFingerSingleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerSingleTap:)] autorelease];
twoFingerSingleTapRecognizer.numberOfTouchesRequired = 2;
[twoFingerSingleTapRecognizer requireGestureRecognizerToFail:twoFingerDoubleTapRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)] autorelease];
[self addGestureRecognizer:singleTapRecognizer];
[self addGestureRecognizer:doubleTapRecognizer];
[self addGestureRecognizer:twoFingerDoubleTapRecognizer];
[self addGestureRecognizer:twoFingerSingleTapRecognizer];
[self addGestureRecognizer:longPressRecognizer];
return self;
}
- (void)dealloc
{
[[mapView tileSource] cancelAllDownloads];
[mapView release]; mapView = nil;
[super dealloc];
}
- (void)layoutSubviews
{
self.contentScaleFactor = 1.0f;
}
-(void)drawRect:(CGRect)rect
{
CGRect bounds = self.bounds;
// NSLog(@"drawRect: {{%f,%f},{%f,%f}}", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
short zoom = log2(bounds.size.width / rect.size.width);
int x = floor(rect.origin.x / rect.size.width), y = floor(fabs(rect.origin.y / rect.size.height));
// NSLog(@"Tile @ x:%d, y:%d, zoom:%d", x, y, zoom);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *tileImage = [[mapView tileSource] imageForTile:RMTileMake(x, y, zoom) inCache:[mapView tileCache]];
[tileImage drawInRect:rect];
[pool release]; pool = nil;
}
#pragma mark -
#pragma mark Event handling
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer
{
if ([delegate respondsToSelector:@selector(mapTiledLayerView:singleTapAtPoint:)])
[delegate mapTiledLayerView:self singleTapAtPoint:[recognizer locationInView:mapView]];
}
- (void)handleTwoFingerSingleTap:(UIGestureRecognizer *)recognizer
{
if ([delegate respondsToSelector:@selector(mapTiledLayerView:twoFingerSingleTapAtPoint:)])
[delegate mapTiledLayerView:self twoFingerSingleTapAtPoint:[recognizer locationInView:mapView]];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state != UIGestureRecognizerStateBegan) return;
if ([delegate respondsToSelector:@selector(mapTiledLayerView:longPressAtPoint:)])
[delegate mapTiledLayerView:self longPressAtPoint:[recognizer locationInView:mapView]];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer
{
if ([delegate respondsToSelector:@selector(mapTiledLayerView:doubleTapAtPoint:)])
[delegate mapTiledLayerView:self doubleTapAtPoint:[recognizer locationInView:mapView]];
}
- (void)handleTwoFingerDoubleTap:(UIGestureRecognizer *)recognizer
{
if ([delegate respondsToSelector:@selector(mapTiledLayerView:twoFingerDoubleTapAtPoint:)])
[delegate mapTiledLayerView:self twoFingerDoubleTapAtPoint:[recognizer locationInView:mapView]];
}
@end