MapState.m
2.7 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
//
// MapState.m
// freemap-iphone
//
// Created by Michel Barakat on 10/20/08.
// Copyright 2008 Høgskolen i Østfold. All rights reserved.
//
#import "MapState.h"
@implementation MapState
@synthesize zoom;
@synthesize tilesPerSide;
@synthesize mapSource;
@synthesize centerCoords;
- (id)initWithMapSource:(MapSource*) initMapSource
CenteredAt:(MapCoordinates*) initCenterCoords
AtZoom:(int) initZoom {
assert(initMapSource != 0);
assert(initCenterCoords != 0);
assert(initZoom >= [initMapSource minZoom] &&
initZoom <= [initMapSource maxZoom]);
mapSource = [[MapSource alloc]
initWithMapDataSource:[initMapSource mapDataSource]];
centerCoords = [[MapCoordinates alloc]
initWithLatitude:[initCenterCoords latitude]
Longitude:[initCenterCoords longitude]];
zoom = initZoom;
tilesPerSide = (1 << zoom); // equivalent to 2^zoom.
boundBoxesSet = FALSE;
return self;
}
- (void)setScreenViewportSize:(CGSize) screenViewportSize
AndMemoryViewportSize:(CGSize) memoryViewportSize {
assert(!boundBoxesSet);
assert(memoryViewportSize.width >= screenViewportSize.width &&
memoryViewportSize.height >= screenViewportSize.height);
// Compute ScreenBoxBoundary.
const double screenHalfHeight = screenViewportSize.height / 2;
const double screenHalfWidth = screenViewportSize.width / 2;
// IMPORTANT: Center point coordinate don't change.
const CGPoint centerPoint = CGPointMake(screenHalfWidth, screenHalfHeight);
screenBoxBoundary.north = centerPoint.y - screenHalfHeight;
screenBoxBoundary.south = centerPoint.y + screenHalfHeight;
screenBoxBoundary.west = centerPoint.x - screenHalfWidth;
screenBoxBoundary.east = centerPoint.x + screenHalfWidth;
// Compute MemoryBoxBoundary.
const double memoryHalfHeight = memoryViewportSize.height / 2;
const double memoryHalfWidth = memoryViewportSize.width / 2;
memoryBoxBoundary.north = centerPoint.y - memoryHalfHeight;
memoryBoxBoundary.south = centerPoint.y + memoryHalfHeight;
memoryBoxBoundary.west = centerPoint.x - memoryHalfWidth;
memoryBoxBoundary.east = centerPoint.x + memoryHalfWidth;
boundBoxesSet = TRUE;
}
- (void)setCenterCoords:(MapCoordinates*) newCenterCoords {
assert(newCenterCoords != 0);
assert(boundBoxesSet);
if (centerCoords != 0) {
[centerCoords release];
}
centerCoords = [[MapCoordinates alloc]
initWithLatitude:[newCenterCoords latitude]
Longitude:[newCenterCoords longitude]];
}
- (struct BoxBoundary)screenViewportBoundary {
assert(boundBoxesSet);
return screenBoxBoundary;
}
- (struct BoxBoundary)memoryViewportBoundary {
assert(boundBoxesSet);
return memoryBoxBoundary;
}
- (void)dealloc {
[mapSource release];
[centerCoords release];
[super dealloc];
}
@end