forked from Incognito/Open-Hamilton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhereare.js
330 lines (303 loc) · 11.4 KB
/
whereare.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
WhereAre
The widget to help build things with google maps JS APIv3 and Static APIv2.
..........................
..........................
..Implementation details..
..........................
..........................
Further documentation assets are located on the GitHub hosting page.... TBA.
Copyright 2011 Brian Graham. All rights reserved.
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function(conf) {
/* This will be required regardless-- todo: 1) name conventions, 2) css reset, 3) see if I can use last-run-script tag instead of document.write()... */
document.write("<div id='mapPlugin'></div>");
/* Retruns object that parses config options and default options from a JSON array.
Attemps to merge: over-ride default with config values, ignore options from config
that wern't defined in defaults.
Works recursively, assignment emulates XOR.
*/
var confMerge= function(conf, defaults) {
//Options will be the new set of values. Need to determine if Options is array or object.
var options = {};
if ((conf == "[object Array]") && (defaults == "[object Array]")) {
//You can't merge an array and an object with each-other.
return conf || defaults;
} else {
if (defaults.length === "undefined") {
//Oprtions is an object.
options = {};
}else {
//Oprtions is an array.
options = [];
}
}
//Cycle through all objects in default
for (var i in defaults) {
//Ensure object exits
if (defaults.hasOwnProperty(i)) {
//If this was listed in the conf
if (conf.hasOwnProperty(i)) {
switch (typeof defaults[i]) {
case "null" :
case "boolean" :
case "number" :
case "string" :
//Deal with bool/num/str
options[i] = conf[i] || defaults[i];
break;
case "function" :
//Deal with functions
options[i] = conf[i] || defaults[i];
break;
case "object" :
//Deal with objects and arrays
options[i] = confMerge(conf[i], defaults[i]) || defaults[i];
break;
default :
case "undefined":
case "xml" :
//Deal with weird stuff
options[i] = null;
break;
}
//If this wasn't listed in the conf, assign default
} else {
options[i] = defaults[i];
}
}
}
return options;
};
/* Returns IMG Node with generated static maps from Google's "Static Maps APIv2"
http://code.google.com/apis/maps/documentation/staticmaps/
Input is documented on GitHub with the rest of this project,
but in the same design of the defaults inside the function.
*/
var buildStaticGoogleMap= function(conf){
var options = confMerge(conf, {
center : [43.24895389686911, -79.86236572265625], //Geographic coordinates (I haven't built in support for address strings)
zoom : 11, //Zoom level on the map.
sensor : false, //Sensor should be false unless on GPS enabled device
size : [100,100], //Pixel size of output map
scale : 1, //Image scale (ie, strech resolution quality)
maptype : "roadmap", //Pick from: roadmap, satellite, terrain, hybrid
//The next four return string literals of whatever was provided, as they can be very complex.
style : "", //http://code.google.com/apis/maps/documentation/staticmaps/#StyledMaps
markers : "", //http://code.google.com/apis/maps/documentation/staticmaps/#Markers
path : "", //http://code.google.com/apis/maps/documentation/staticmaps/#Paths
visible : "" //http://code.google.com/apis/maps/documentation/staticmaps/#Viewports
});
var baseURL = "http://maps.googleapis.com/maps/api/staticmap?";
for (var i in options) {
if (options.hasOwnProperty(i)) {
baseURL += i + "=";
//Manage special case encoding, ie, center is one param that is encoded as 100x100, rather than individual x/y coords.
switch (i) {
case "center":
baseURL += options[i][0] + "," +options[i][1];
break;
case "size":
baseURL += options[i][0] + "x" +options[i][1];
break;
default:
baseURL += options[i];
}
baseURL += "&";
}
}
//Crete the dom node and return the node to be used elsewhere.
var ele;
ele = document.createElement("img");
ele.src= baseURL;
ele.alt= "Loading static map preloader";
return ele;
};
/* Configure display options */
var options = confMerge(conf, {
/* Configure layers to put in this impelmentation
Layers are an array of objects that contain groups of data sets
Groups hold arrays of data lables and Fusion Table IDs
*/
"layers" : [
{ "name" : "Water",
"data" : [
{
"name" : "Beach",
"table" : 1
},
{
"name" : "Outdoor Pool",
"table" : 1
},
{
"name" : "Indoor Pool",
"table" : 1
},
{
"name" : "Splash Pad",
"table" : 1
},
{
"name" : "Wading Pool",
"table" : 1
}
]},
{ "name" : "Parks",
"data" : [
{
"name" : "Parks",
"table" : 1
},
{
"name" : "Waterfalls",
"table" : 1
},
{
"name" : "Conservation",
"table" : 1
},
{
"name" : "Trails",
"table" : 1
}
]},
{ "name" : "Bus Stops",
"data" : [
{
"name" : "HSR",
"table" : 1
},
{
"name" : "B-Line",
"table" : 1
},
{
"name" : "GO",
"table" : 1
}
]}
],
/* Configure display options */
"display" : {
"height" : 400, /* Height as Pixels as an int */
"width" : 960, /* Width as Pixels as an int */
"sideBarWidth" : (25/100), /* Percentage of width.height to be used for side-bar (decimals between 0 and 1.)*/
"sideBarPosition" : "left", /* "left", "right", "top", "bottom" of the box, where the map appears opposite to this */
"style" : "white" /* Style of the box. See style object for style types, or import your own style. */
},
/* Configuration for google map at time of intialization */
"map" : {
"zoom" : 11,
"center" : [43.24895389686911, -79.86236572265625]
},
/* functions for callback features */
"callbacks" : {
"before" : function(){},
"success" : function(){},
"error" : function(){}
}
});
/*
Todo: Build the divs based on settings.
load from image API first...
when JS api is ready, replace... (if device supports google interactive maps).
*/
var sideBarSize = [];
var mapSize = [];
switch (options.display.sideBarPosition) {
case "right" :
mapSize[0] = (Math.floor(options.display.width * (1 - options.display.sideBarWidth)));
mapSize[1] = (options.display.height);
sideBarSize[0] = (Math.ceil(options.display.width * options.display.sideBarWidth));
sideBarSize[1] = (options.display.height);
break;
case "top" :
mapSize[0] = (Math.floor(options.display.height * (1 - options.display.sideBarWidth)));
mapSize[1] = (options.display.width);
sideBarSize[0] = (Math.ceil(options.display.height * options.display.sideBarWidth));
sideBarSize[1] = (options.display.width);
break;
case "bottom":
mapSize[0] = (Math.floor(options.display.height * (1 - options.display.sideBarWidth)));
mapSize[1] = (options.display.width);
sideBarSize[0] = (Math.ceil(options.display.height * options.display.sideBarWidth));
sideBarSize[1] = (options.display.width);
break;
default:
case "left" :
mapSize[0] = (Math.floor(options.display.width * (1 - options.display.sideBarWidth)));
mapSize[1] = (options.display.height);
sideBarSize[0] = (Math.ceil(options.display.width * options.display.sideBarWidth));
sideBarSize[1] = (options.display.height);
break;
}
/*Base elements that will hold the widget */
var baseEle = document.createDocumentFragment();
var staticMapIMG = buildStaticGoogleMap({
center : options.map.center,
zoom : options.map.zoom,
size : mapSize
});
var mapHolder = document.createElement("div");
var sideBar = document.createElement("div");
sideBar.className = "mapControls"
mapHolder.className = "mapViewport"
mapHolder.appendChild(staticMapIMG);
switch (options.display.sideBarPosition) {
case "top" :
case "left" :
baseEle.appendChild(sideBar);
baseEle.appendChild(mapHolder);
break;
default:
case "bottom":
case "right" :
baseEle.appendChild(mapHolder);
baseEle.appendChild(sideBar);
break;
}
/*Generate menu HTML and buttons, returns element to append child to sideBar*/
sideBar.appendChild((function() {
var subList, listItem, subListItem;
var mainList = document.createElement("ul");
for (var i=0; i<options.layers.length; i++) {
listItem = document.createElement("li");
listItem.appendChild(document.createTextNode(options.layers[i].name));
subList = document.createElement("ul")
for (var j=0; j<options.layers[i].data.length; j++) {
subListItem = document.createElement("li");
subListItem.appendChild(document.createTextNode(options.layers[i].data[j].name));
subList.appendChild(subListItem);
}
listItem.appendChild(subList)
mainList.appendChild(listItem);
}
return mainList
})());
/*
1) beam in content
2) run conf or defaults
3) draw divs -- Can I check script elements instead of running doc write? Check that...
4) Maps initalize
5) Layers
6) ????
7) Profit!
*/
document.getElementById("mapPlugin").appendChild(baseEle);
})({
//Configuration
});