forked from react-native-maps/react-native-maps
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAIRMapWMSTile.m
More file actions
149 lines (127 loc) · 4.36 KB
/
AIRMapWMSTile.m
File metadata and controls
149 lines (127 loc) · 4.36 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
141
142
143
144
145
146
147
148
149
//
// AIRMapWMSTile.m
// AirMaps
//
// Created by nizam on 10/28/18.
// Copyright © 2018. All rights reserved.
//
#import "AIRMapWMSTile.h"
#import <React/UIView+React.h>
@implementation AIRMapWMSTile {
BOOL _urlTemplateSet;
}
- (void)setShouldReplaceMapContent:(BOOL)shouldReplaceMapContent
{
_shouldReplaceMapContent = shouldReplaceMapContent;
if(self.tileOverlay) {
self.tileOverlay.canReplaceMapContent = _shouldReplaceMapContent;
}
[self update];
}
- (void)setMaximumZ:(NSUInteger)maximumZ
{
_maximumZ = maximumZ;
if(self.tileOverlay) {
self.tileOverlay.maximumZ = _maximumZ;
}
[self update];
}
- (void)setMinimumZ:(NSUInteger)minimumZ
{
_minimumZ = minimumZ;
if(self.tileOverlay) {
self.tileOverlay.minimumZ = _minimumZ;
}
[self update];
}
- (void)setTileSize:(NSInteger)tileSize
{
_tileSize = tileSize;
if(self.tileOverlay) {
self.tileOverlay.tileSize = CGSizeMake(tileSize, tileSize);
}
[self update];
}
- (void)setUrlTemplate:(NSString *)urlTemplate{
_urlTemplate = urlTemplate;
_urlTemplateSet = YES;
[self createTileOverlayAndRendererIfPossible];
[self update];
}
- (void) createTileOverlayAndRendererIfPossible
{
if (!_urlTemplateSet) return;
self.tileOverlay = [[TileOverlay alloc] initWithURLTemplate:self.urlTemplate];
self.tileOverlay.canReplaceMapContent = self.shouldReplaceMapContent;
if(self.minimumZ) {
self.tileOverlay.minimumZ = self.minimumZ;
}
if (self.maximumZ) {
self.tileOverlay.maximumZ = self.maximumZ;
}
if (self.tileSize) {
self.tileOverlay.tileSize = CGSizeMake(self.tileSize, self.tileSize);;
}
self.renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:self.tileOverlay];
}
- (void) update
{
if (!_renderer) return;
if (_map == nil) return;
[_map removeOverlay:self];
[_map addOverlay:self level:MKOverlayLevelAboveLabels];
for (id<MKOverlay> overlay in _map.overlays) {
if ([overlay isKindOfClass:[AIRMapWMSTile class]]) {
continue;
}
[_map removeOverlay:overlay];
[_map addOverlay:overlay];
}
}
#pragma mark MKOverlay implementation
- (CLLocationCoordinate2D) coordinate
{
return self.tileOverlay.coordinate;
}
- (MKMapRect) boundingMapRect
{
return self.tileOverlay.boundingMapRect;
}
- (BOOL)canReplaceMapContent
{
return self.tileOverlay.canReplaceMapContent;
}
@end
@implementation TileOverlay
@synthesize MapX;
@synthesize MapY;
@synthesize FULL;
-(id) initWithURLTemplate:(NSString *)URLTemplate {
self = [super initWithURLTemplate:URLTemplate];
MapX = -20037508.34789244;
MapY = 20037508.34789244;
FULL = 20037508.34789244 * 2;
return self ;
}
-(NSURL *)URLForTilePath:(MKTileOverlayPath)path{
NSArray *bb = [self getBoundBox:path.x yAxis:path.y zoom:path.z];
NSMutableString *url = [self.URLTemplate mutableCopy];
[url replaceOccurrencesOfString: @"{minX}" withString:[NSString stringWithFormat:@"%@", bb[0]] options:0 range:NSMakeRange(0, url.length)];
[url replaceOccurrencesOfString: @"{minY}" withString:[NSString stringWithFormat:@"%@", bb[1]] options:0 range:NSMakeRange(0, url.length)];
[url replaceOccurrencesOfString: @"{maxX}" withString:[NSString stringWithFormat:@"%@", bb[2]] options:0 range:NSMakeRange(0, url.length)];
[url replaceOccurrencesOfString: @"{maxY}" withString:[NSString stringWithFormat:@"%@", bb[3]] options:0 range:NSMakeRange(0, url.length)];
[url replaceOccurrencesOfString: @"{width}" withString:[NSString stringWithFormat:@"%d", (int)self.tileSize.width] options:0 range:NSMakeRange(0, url.length)];
[url replaceOccurrencesOfString: @"{height}" withString:[NSString stringWithFormat:@"%d", (int)self.tileSize.height] options:0 range:NSMakeRange(0, url.length)];
return [NSURL URLWithString:url];
}
-(NSArray *)getBoundBox:(NSInteger)x yAxis:(NSInteger)y zoom:(NSInteger)zoom{
double tile = FULL / pow(2.0, (double)zoom);
NSArray *result =[[NSArray alloc] initWithObjects:
[NSNumber numberWithDouble:MapX + (double)x * tile ],
[NSNumber numberWithDouble:MapY - (double)(y+1) * tile ],
[NSNumber numberWithDouble:MapX + (double)(x+1) * tile ],
[NSNumber numberWithDouble:MapY - (double)y * tile ],
nil];
return result;
}
@end