Subversion Repositories Applications.papyrus

Rev

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