1//
2//  AIRGoogleMapURLTile.m
3//  Created by Nick Italiano on 11/5/16.
4//
5
6#ifdef HAVE_GOOGLE_MAPS
7
8#import "AIRGoogleMapUrlTile.h"
9
10@implementation AIRGoogleMapUrlTile
11
12- (void)setZIndex:(int)zIndex
13{
14  _zIndex = zIndex;
15  _tileLayer.zIndex = zIndex;
16}
17
18- (void)setUrlTemplate:(NSString *)urlTemplate
19{
20  _urlTemplate = urlTemplate;
21  _tileLayer = [GMSURLTileLayer tileLayerWithURLConstructor:[self _getTileURLConstructor]];
22  _tileLayer.tileSize = [[UIScreen mainScreen] scale] * 256;
23}
24
25- (GMSTileURLConstructor)_getTileURLConstructor
26{
27  NSString *urlTemplate = self.urlTemplate;
28  NSInteger *maximumZ = self.maximumZ;
29  NSInteger *minimumZ = self.minimumZ;
30  GMSTileURLConstructor urls = ^NSURL* _Nullable (NSUInteger x, NSUInteger y, NSUInteger zoom) {
31
32    if (self.flipY == YES) {
33      y = (1 << zoom) - y - 1;
34    }
35
36    NSString *url = urlTemplate;
37    url = [url stringByReplacingOccurrencesOfString:@"{x}" withString:[NSString stringWithFormat: @"%ld", (long)x]];
38    url = [url stringByReplacingOccurrencesOfString:@"{y}" withString:[NSString stringWithFormat: @"%ld", (long)y]];
39    url = [url stringByReplacingOccurrencesOfString:@"{z}" withString:[NSString stringWithFormat: @"%ld", (long)zoom]];
40
41   if(maximumZ && (long)zoom > (long)maximumZ) {
42      return nil;
43    }
44
45    if(minimumZ && (long)zoom < (long)minimumZ) {
46      return nil;
47    }
48
49    return [NSURL URLWithString:url];
50  };
51  return urls;
52}
53
54@end
55
56#endif
57