1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.widget.GoogleMap");
|
|
|
14 |
dojo.require("dojo.event.*");
|
|
|
15 |
dojo.require("dojo.math");
|
|
|
16 |
dojo.require("dojo.widget.*");
|
|
|
17 |
dojo.require("dojo.uri.Uri");
|
|
|
18 |
dojo.require("dojo.widget.HtmlWidget");
|
|
|
19 |
(function () {
|
|
|
20 |
var gkey = djConfig["gMapKey"] || djConfig["googleMapKey"];
|
|
|
21 |
var uri = new dojo.uri.Uri(window.location.href);
|
|
|
22 |
if (uri.host == "www.dojotoolkit.org") {
|
|
|
23 |
gkey = "ABQIAAAACUNdgv_7FGOmUslbm9l6_hRqjp7ri2mNiOEYqetD3xnFHpt5rBSjszDd1sdufPyQKUTyCf_YxoIxvw";
|
|
|
24 |
} else {
|
|
|
25 |
if (uri.host == "blog.dojotoolkit.org") {
|
|
|
26 |
gkey = "ABQIAAAACUNdgv_7FGOmUslbm9l6_hSkep6Av1xaMhVn3yCLkorJeXeLARQ6fammI_P3qSGleTJhoI5_1JmP_Q";
|
|
|
27 |
} else {
|
|
|
28 |
if (uri.host == "archive.dojotoolkit.org") {
|
|
|
29 |
gkey = "ABQIAAAACUNdgv_7FGOmUslbm9l6_hTaQpDt0dyGLIHbXMPTzg1kWeAfwRTwZNyrUfbfxYE9yIvRivEjcXoDTg";
|
|
|
30 |
} else {
|
|
|
31 |
if (uri.host == "dojotoolkit.org") {
|
|
|
32 |
gkey = "ABQIAAAACUNdgv_7FGOmUslbm9l6_hSaOaO_TgJ5c3mtQFnk5JO2zD5dZBRZk-ieqVs7BORREYNzAERmcJoEjQ";
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
}
|
|
|
37 |
if (!dojo.hostenv.post_load_) {
|
|
|
38 |
if (!gkey || gkey == "") {
|
|
|
39 |
dojo.raise("dojo.widget.GoogleMap: The Google Map widget requires a proper API key in order to be used.");
|
|
|
40 |
}
|
|
|
41 |
var tag = "<scr" + "ipt src='http://maps.google.com/maps?file=api&v=2&key=" + gkey + "'></scri" + "pt>";
|
|
|
42 |
if (!dj_global["GMap2"]) {
|
|
|
43 |
document.write(tag);
|
|
|
44 |
}
|
|
|
45 |
} else {
|
|
|
46 |
dojo.debug("Cannot initialize Google Map system after the page has been loaded! Please either manually include the script block provided by Google in your page or require() the GoogleMap widget before onload has fired.");
|
|
|
47 |
}
|
|
|
48 |
})();
|
|
|
49 |
dojo.widget.defineWidget("dojo.widget.GoogleMap", dojo.widget.HtmlWidget, function () {
|
|
|
50 |
this.map = null;
|
|
|
51 |
this.geocoder = null;
|
|
|
52 |
this.data = [];
|
|
|
53 |
this.datasrc = "";
|
|
|
54 |
this.controls = ["largemap", "scale", "maptype"];
|
|
|
55 |
}, {templatePath:null, templateCssPath:null, isContainer:false, _defaultPoint:{lat:39.10662, lng:-94.578209}, setControls:function () {
|
|
|
56 |
var methodmap = {largemap:GLargeMapControl, smallmap:GSmallMapControl, smallzoom:GSmallZoomControl, scale:GScaleControl, maptype:GMapTypeControl, overview:GOverviewMapControl};
|
|
|
57 |
for (var i = 0; i < this.controls.length; i++) {
|
|
|
58 |
this.map.addControl(new (methodmap[this.controls[i].toLowerCase()])());
|
|
|
59 |
}
|
|
|
60 |
}, findCenter:function (bounds) {
|
|
|
61 |
if (this.data.length == 1) {
|
|
|
62 |
return (new GLatLng(this.data[0].lat, this.data[0].lng));
|
|
|
63 |
}
|
|
|
64 |
var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
|
|
|
65 |
var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
|
|
|
66 |
return (new GLatLng(clat, clng));
|
|
|
67 |
}, createPinpoint:function (pt, overlay) {
|
|
|
68 |
var m = new GMarker(pt);
|
|
|
69 |
if (overlay) {
|
|
|
70 |
GEvent.addListener(m, "click", function () {
|
|
|
71 |
m.openInfoWindowHtml("<div>" + overlay + "</div>");
|
|
|
72 |
});
|
|
|
73 |
}
|
|
|
74 |
return m;
|
|
|
75 |
}, plot:function (obj) {
|
|
|
76 |
var p = new GLatLng(obj.lat, obj.lng);
|
|
|
77 |
var d = obj.description || null;
|
|
|
78 |
var m = this.createPinpoint(p, d);
|
|
|
79 |
this.map.addOverlay(m);
|
|
|
80 |
}, plotAddress:function (address) {
|
|
|
81 |
var self = this;
|
|
|
82 |
this.geocoder.getLocations(address, function (response) {
|
|
|
83 |
if (!response || response.Status.code != 200) {
|
|
|
84 |
alert("The address \"" + address + "\" was not found.");
|
|
|
85 |
return;
|
|
|
86 |
}
|
|
|
87 |
var obj = {lat:response.Placemark[0].Point.coordinates[1], lng:response.Placemark[0].Point.coordinates[0], description:response.Placemark[0].address};
|
|
|
88 |
self.data.push(obj);
|
|
|
89 |
self.render();
|
|
|
90 |
});
|
|
|
91 |
}, parse:function (table) {
|
|
|
92 |
this.data = [];
|
|
|
93 |
var h = table.getElementsByTagName("thead")[0];
|
|
|
94 |
if (!h) {
|
|
|
95 |
return;
|
|
|
96 |
}
|
|
|
97 |
var a = [];
|
|
|
98 |
var cols = h.getElementsByTagName("td");
|
|
|
99 |
if (cols.length == 0) {
|
|
|
100 |
cols = h.getElementsByTagName("th");
|
|
|
101 |
}
|
|
|
102 |
for (var i = 0; i < cols.length; i++) {
|
|
|
103 |
var c = cols[i].innerHTML.toLowerCase();
|
|
|
104 |
if (c == "long") {
|
|
|
105 |
c = "lng";
|
|
|
106 |
}
|
|
|
107 |
a.push(c);
|
|
|
108 |
}
|
|
|
109 |
var b = table.getElementsByTagName("tbody")[0];
|
|
|
110 |
if (!b) {
|
|
|
111 |
return;
|
|
|
112 |
}
|
|
|
113 |
for (var i = 0; i < b.childNodes.length; i++) {
|
|
|
114 |
if (!(b.childNodes[i].nodeName && b.childNodes[i].nodeName.toLowerCase() == "tr")) {
|
|
|
115 |
continue;
|
|
|
116 |
}
|
|
|
117 |
var cells = b.childNodes[i].getElementsByTagName("td");
|
|
|
118 |
var o = {};
|
|
|
119 |
for (var j = 0; j < a.length; j++) {
|
|
|
120 |
var col = a[j];
|
|
|
121 |
if (col == "lat" || col == "lng") {
|
|
|
122 |
o[col] = parseFloat(cells[j].innerHTML);
|
|
|
123 |
} else {
|
|
|
124 |
o[col] = cells[j].innerHTML;
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
this.data.push(o);
|
|
|
128 |
}
|
|
|
129 |
}, render:function () {
|
|
|
130 |
if (this.data.length == 0) {
|
|
|
131 |
this.map.setCenter(new GLatLng(this._defaultPoint.lat, this._defaultPoint.lng), 4);
|
|
|
132 |
return;
|
|
|
133 |
}
|
|
|
134 |
this.map.clearOverlays();
|
|
|
135 |
var bounds = new GLatLngBounds();
|
|
|
136 |
var d = this.data;
|
|
|
137 |
for (var i = 0; i < d.length; i++) {
|
|
|
138 |
bounds.extend(new GLatLng(d[i].lat, d[i].lng));
|
|
|
139 |
}
|
|
|
140 |
var zoom = Math.min((this.map.getBoundsZoomLevel(bounds) - 1), 14);
|
|
|
141 |
this.map.setCenter(this.findCenter(bounds), zoom);
|
|
|
142 |
for (var i = 0; i < this.data.length; i++) {
|
|
|
143 |
this.plot(this.data[i]);
|
|
|
144 |
}
|
|
|
145 |
}, initialize:function (args, frag) {
|
|
|
146 |
if (this.datasrc) {
|
|
|
147 |
this.parse(dojo.byId(this.datasrc));
|
|
|
148 |
} else {
|
|
|
149 |
if (this.domNode.getElementsByTagName("table")[0]) {
|
|
|
150 |
this.parse(this.domNode.getElementsByTagName("table")[0]);
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
}, postCreate:function () {
|
|
|
154 |
while (this.domNode.childNodes.length > 0) {
|
|
|
155 |
this.domNode.removeChild(this.domNode.childNodes[0]);
|
|
|
156 |
}
|
|
|
157 |
if (this.domNode.style.position != "absolute") {
|
|
|
158 |
this.domNode.style.position = "relative";
|
|
|
159 |
}
|
|
|
160 |
this.map = new GMap2(this.domNode);
|
|
|
161 |
try {
|
|
|
162 |
this.geocoder = new GClientGeocoder();
|
|
|
163 |
}
|
|
|
164 |
catch (ex) {
|
|
|
165 |
}
|
|
|
166 |
this.render();
|
|
|
167 |
this.setControls();
|
|
|
168 |
}});
|
|
|
169 |
|