Subversion Repositories Applications.papyrus

Rev

Rev 1422 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
		}
2149 mathias 41
		//var tag = "<scr" + "ipt src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=" + gkey + "'></scri" + "pt>";
42
		var tag = "<scr" + "ipt src='http://maps.googleapis.com/maps/api/js?sensor=false'></scri" + "pt>";
1318 alexandre_ 43
		if (!dj_global["GMap2"]) {
44
			document.write(tag);
45
		}
46
	} else {
47
		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.");
48
	}
49
})();
50
dojo.widget.defineWidget("dojo.widget.GoogleMap", dojo.widget.HtmlWidget, function () {
51
	this.map = null;
52
	this.geocoder = null;
53
	this.data = [];
54
	this.datasrc = "";
55
	this.controls = ["largemap", "scale", "maptype"];
56
}, {templatePath:null, templateCssPath:null, isContainer:false, _defaultPoint:{lat:39.10662, lng:-94.578209}, setControls:function () {
57
	var methodmap = {largemap:GLargeMapControl, smallmap:GSmallMapControl, smallzoom:GSmallZoomControl, scale:GScaleControl, maptype:GMapTypeControl, overview:GOverviewMapControl};
58
	for (var i = 0; i < this.controls.length; i++) {
59
		this.map.addControl(new (methodmap[this.controls[i].toLowerCase()])());
60
	}
61
}, findCenter:function (bounds) {
62
	if (this.data.length == 1) {
63
		return (new GLatLng(this.data[0].lat, this.data[0].lng));
64
	}
65
	var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
66
	var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
67
	return (new GLatLng(clat, clng));
68
}, createPinpoint:function (pt, overlay) {
69
	var m = new GMarker(pt);
70
	if (overlay) {
71
		GEvent.addListener(m, "click", function () {
72
			m.openInfoWindowHtml("<div>" + overlay + "</div>");
73
		});
74
	}
75
	return m;
76
}, plot:function (obj) {
77
	var p = new GLatLng(obj.lat, obj.lng);
78
	var d = obj.description || null;
79
	var m = this.createPinpoint(p, d);
80
	this.map.addOverlay(m);
81
}, plotAddress:function (address) {
82
	var self = this;
83
	this.geocoder.getLocations(address, function (response) {
84
		if (!response || response.Status.code != 200) {
85
			alert("The address \"" + address + "\" was not found.");
86
			return;
87
		}
88
		var obj = {lat:response.Placemark[0].Point.coordinates[1], lng:response.Placemark[0].Point.coordinates[0], description:response.Placemark[0].address};
89
		self.data.push(obj);
90
		self.render();
91
	});
92
}, parse:function (table) {
93
	this.data = [];
94
	var h = table.getElementsByTagName("thead")[0];
95
	if (!h) {
96
		return;
97
	}
98
	var a = [];
99
	var cols = h.getElementsByTagName("td");
100
	if (cols.length == 0) {
101
		cols = h.getElementsByTagName("th");
102
	}
103
	for (var i = 0; i < cols.length; i++) {
104
		var c = cols[i].innerHTML.toLowerCase();
105
		if (c == "long") {
106
			c = "lng";
107
		}
108
		a.push(c);
109
	}
110
	var b = table.getElementsByTagName("tbody")[0];
111
	if (!b) {
112
		return;
113
	}
114
	for (var i = 0; i < b.childNodes.length; i++) {
115
		if (!(b.childNodes[i].nodeName && b.childNodes[i].nodeName.toLowerCase() == "tr")) {
116
			continue;
117
		}
118
		var cells = b.childNodes[i].getElementsByTagName("td");
119
		var o = {};
120
		for (var j = 0; j < a.length; j++) {
121
			var col = a[j];
122
			if (col == "lat" || col == "lng") {
123
				o[col] = parseFloat(cells[j].innerHTML);
124
			} else {
125
				o[col] = cells[j].innerHTML;
126
			}
127
		}
128
		this.data.push(o);
129
	}
130
}, render:function () {
131
	if (this.data.length == 0) {
132
		this.map.setCenter(new GLatLng(this._defaultPoint.lat, this._defaultPoint.lng), 4);
133
		return;
134
	}
135
	this.map.clearOverlays();
136
	var bounds = new GLatLngBounds();
137
	var d = this.data;
138
	for (var i = 0; i < d.length; i++) {
139
		bounds.extend(new GLatLng(d[i].lat, d[i].lng));
140
	}
141
	var zoom = Math.min((this.map.getBoundsZoomLevel(bounds) - 1), 14);
142
	this.map.setCenter(this.findCenter(bounds), zoom);
143
	for (var i = 0; i < this.data.length; i++) {
144
		this.plot(this.data[i]);
145
	}
146
}, initialize:function (args, frag) {
147
	if (this.datasrc) {
148
		this.parse(dojo.byId(this.datasrc));
149
	} else {
150
		if (this.domNode.getElementsByTagName("table")[0]) {
151
			this.parse(this.domNode.getElementsByTagName("table")[0]);
152
		}
153
	}
154
}, postCreate:function () {
155
	while (this.domNode.childNodes.length > 0) {
156
		this.domNode.removeChild(this.domNode.childNodes[0]);
157
	}
158
	if (this.domNode.style.position != "absolute") {
159
		this.domNode.style.position = "relative";
160
	}
161
	this.map = new GMap2(this.domNode);
162
	try {
163
		this.geocoder = new GClientGeocoder();
164
	}
165
	catch (ex) {
166
	}
167
	this.render();
168
	this.setControls();
169
}});
170