Repository navigation
Expand file tree
/
Copy pathcopyright.js
More file actions
127 lines (118 loc) · 4.09 KB
/
Copy pathcopyright.js
File metadata and controls
127 lines (118 loc) · 4.09 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
/**
* @name Copyright and CopyrightCollection classes for Google Maps JS API v3.
* @author Jennifer Chye
* @fileoverview
* This library contains classes for working with copyrights for custom map
* types.
* <br/>
* This is a v3 implementation of the <a href="http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GCopyright">GCopyright</a>
* and <a href="http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GCopyrightCollection">GCopyrightCollection</a>
* classes in v2 of the Maps API.
*/
/**
* A copyright is defined by the highest zoom level at which it should be
* shown, the lat/lng bounds of the content it relates to, and the text
* associated with the copyright.
*
* @param {string} id Unique id.
* @param {google.maps.LatLngBounds} bounds Bounds to which this copyright
* applies.
* @param {number} minZoom Minimum zoom at which to display.
* @param {string} text Copyright text.
* @constructor
*/
function Copyright(id, bounds, minZoom, text) {
this.id = id;
this.bounds = bounds;
this.minZoom = minZoom;
this.text = text;
}
/**
* Creates a copyright collection for a single map type/spec.
*
* @param {string} opt_prefix Prefix for copyrights (optional).
* @constructor
*/
function CopyrightCollection(opt_prefix) {
this.zoomLevels_ = [];
this.copyrights_ = {};
this.prefix_ = opt_prefix || '';
}
/**
* Adds the given copyright to the collection, returning true if the copyright
* was new and was added or false if the copyright was a duplicate and ignored.
*
* @param {Copyright} copyright New copyright to add.
*/
CopyrightCollection.prototype.addCopyright = function(copyright) {
if (this.copyrights_[copyright.id]) {
return false;
}
// Add enough zoom levels to accomodate this copyright.
var zoomLevels = this.zoomLevels_;
var zoom = copyright.minZoom;
while (zoomLevels.length <= zoom) {
zoomLevels.push([]);
}
this.copyrights_[copyright.id] = 1;
zoomLevels[zoom].push(copyright);
return true;
};
/**
* Returns an array of copyrights we have available for a given point.
* @param {google.maps.LatLng} point The point for which we want to get
* copyrights.
* @return {Array.<Copyright>} Resulting array of copyrights.
*/
CopyrightCollection.prototype.getCopyrightsAtPoint = function(point) {
var ret = [];
var zoomLevels = this.zoomLevels_;
for (var i = 0; i < zoomLevels.length; i++) {
for (var j = 0; j < zoomLevels[i].length; j++) {
var copyright = zoomLevels[i][j];
if (copyright.bounds.contains(point)) {
ret.push(copyright);
}
}
}
return ret;
};
/**
* Returns an array of the copyrights we should display for the given viewport.
*
* @param {google.maps.LatLngBounds} bounds Query bounds.
* @param {number} zoom Zoom level.
* @return {Array.<string>} Resulting array of copyright strings.
*/
CopyrightCollection.prototype.getCopyrights = function(bounds, zoom) {
var seenCopyrights = {};
var copyrights = [];
// We start iterating from the given zoom level, which skips all copyrights
// that are only displayed at higher zoom levels.
var zoomLevels = this.zoomLevels_;
for (var i = Math.min(zoom, zoomLevels.length - 1); i >= 0; i--) {
var copyrightList = zoomLevels[i];
for (var j = 0; j < copyrightList.length; j++) {
var copyright = copyrightList[j];
var text = copyright.text;
// Don't include duplicate copyrights.
if (copyright.bounds.intersects(bounds) && !seenCopyrights[text]) {
copyrights.push(text);
seenCopyrights[text] = 1;
}
}
}
return copyrights;
};
/**
* Returns a string containing the prefix and copyright texts for this bounds.
*
* @param {google.maps.LatLngBounds} bounds Query bounds.
* @param {number} zoom Zoom level.
* @return {string} Resulting Copyright notice, or an empty string if there were
* no copyright notices in the provided bounds.
*/
CopyrightCollection.prototype.getCopyrightNotice = function(bounds, zoom) {
var copyrights = this.getCopyrights(bounds, zoom);
return copyrights.length ? this.prefix_ + ' ' + copyrights.join(', ') : '';
};