#define M_PI 3.14159265358979323846264338327950288
- (NSUInteger)tileCountForSouthWest:(CLLocationCoordinate2D)southWest northEast:(CLLocationCoordinate2D)northEast minZoom:(NSUInteger)minZoom maxZoom:(NSUInteger)maxZoom
{
NSUInteger minCacheZoom = minZoom;
NSUInteger maxCacheZoom = maxZoom;
CLLocationDegrees minCacheLat = southWest.latitude;
CLLocationDegrees maxCacheLat = northEast.latitude;
CLLocationDegrees minCacheLon = southWest.longitude;
CLLocationDegrees maxCacheLon = northEast.longitude;
NSAssert(minCacheZoom <= maxCacheZoom, @"Minimum zoom should be less than or equal to maximum zoom");
NSAssert(maxCacheLat > minCacheLat, @"Northernmost bounds should exceed southernmost bounds");
NSAssert(maxCacheLon > minCacheLon, @"Easternmost bounds should exceed westernmost bounds");
NSUInteger n, xMin, yMax, xMax, yMin;
NSUInteger totalTiles = 0;
for (NSUInteger zoom = minCacheZoom; zoom <= maxCacheZoom; zoom++)
{
n = pow(2.0, zoom);
xMin = floor(((minCacheLon + 180.0) / 360.0) * n);
yMax = floor((1.0 - (logf(tanf(minCacheLat * M_PI / 180.0) + 1.0 / cosf(minCacheLat * M_PI / 180.0)) / M_PI)) / 2.0 * n);
xMax = floor(((maxCacheLon + 180.0) / 360.0) * n);
yMin = floor((1.0 - (logf(tanf(maxCacheLat * M_PI / 180.0) + 1.0 / cosf(maxCacheLat * M_PI / 180.0)) / M_PI)) / 2.0 * n);
totalTiles += (xMax + 1 - xMin) * (yMax + 1 - yMin);
}
return totalTiles;
}
그리고 한번에 다운받는 부분은 Mapbox Offine 샘플에서 가져왔는데 대략적인것만 붙이겠습니다.
- (void)mapImageDownload:(NSInteger)totalCount {
CGSize size = self.bounds.size;
CGPoint point = {0, 0};
point.x = size.width;
CLLocationCoordinate2D northEast = [_googleMap.projection coordinateForPoint:point];
point.x = 0;
point.y = size.height;
CLLocationCoordinate2D southWest = [_googleMap.projection coordinateForPoint:point];
NSUInteger minZoom = zoomLevel;
NSUInteger maxZoom = 19;
NSUInteger minCacheZoom = minZoom;
NSUInteger maxCacheZoom = maxZoom;
CLLocationDegrees minCacheLat = southWest.latitude;
CLLocationDegrees maxCacheLat = northEast.latitude;
CLLocationDegrees minCacheLon = southWest.longitude;
CLLocationDegrees maxCacheLon = northEast.longitude;
dispatch_queue_t dQueue = dispatch_queue_create("MapDownload", NULL);
dispatch_async(dQueue, ^{
NSUInteger n, xMin, yMax, xMax, yMin;
NSString *documentPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSUInteger zoom = minCacheZoom; zoom <= maxCacheZoom; zoom++) {
n = pow(2.0, zoom);
xMin = floor(((minCacheLon + 180.0) / 360.0) * n);
yMax = floor((1.0 - (logf(tanf(minCacheLat * M_PI / 180.0) + 1.0 / cosf(minCacheLat * M_PI / 180.0)) / M_PI)) / 2.0 * n);
xMax = floor(((maxCacheLon + 180.0) / 360.0) * n);
yMin = floor((1.0 - (logf(tanf(maxCacheLat * M_PI / 180.0) + 1.0 / cosf(maxCacheLat * M_PI / 180.0)) / M_PI)) / 2.0 * n);
for (NSUInteger x = xMin; x <= xMax; x++) {
for (NSUInteger y = yMin; y <= yMax; y++) {
NSString *stringUrl = [NSString stringWithFormat:@"http://tile.openstreetmap.org/%zd/%zd/%zd.png", zoom, x, y];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringUrl]];
어쩌고 저쩌고 하면 되겠습니다.
제가 이것 때문에 고민이 많았는데 누군가에게 도움이 되었으면 좋겠습니다.
아 그런데 이렇게 하면 메모리를 좀 더 많이 쓰는 느낌이 드네요. ㅎㅎ
의견 있으시면 댓글 부탁드립니다.
'iOS > objective-c' 카테고리의 다른 글
근접센서 화면 꺼짐 (0) | 2018.12.12 |
---|---|
기기설정 관련없이 언어셋 변경 (0) | 2018.12.12 |
[공유] iOS9 Contacts.framework 연락처 불러오기(상세) (0) | 2018.12.12 |
[공유] UIAlertController 간단한 코드입니다 (0) | 2018.12.12 |
nsstring selector 지정 (0) | 2018.12.12 |