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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Open Street Map</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<style type="text/css" media="all">
html, body, #map {
width: 100%;
height: 100%;
margin: 0;
position: relative;
z-index: 0;
font-family: sans-serif;
}
#search_form {
position: absolute;
z-index: 1;
bottom: 2.5em;
right: 0.5em;
}
#layout_switch {
position: absolute;
bottom: 15px;
left: 140px;
z-index: 1;
}
#layout_switch a {
color: black;
}
#layout_switch a:visited {
color: black;
}
#layout_switch a:hover {
color: white;
background-color: black;
opacity: 0.5;
}
.olControlPanZoom {
top: auto !important;
bottom: 173px; /* .olControlScaleLine.bottom = 15 + .olControlScaleLine.bottom = 26 + margin = 15px + #OpenLayers.Control.PanZoom_5_zoomout = 99 + #OpenLayers.Control.PanZoom_5_zoomout = 18 */
}
.olControlAttribution {
right: 0.5em !important;
bottom: 15px !important;
}
</style>
<script src="api/OpenLayers.js" type="text/javascript"></script>
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
/*
* OpenStreetMap Widget
* ==========================================================
* Copyright (C) Université Claude Bernard Lyon 1 (ICAP), Délégation Interministérielle à l'Éducation Numérique en Afrique, Guillaume Burel, 2012
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* API identifier */
var geonamesUser = "yimgo";
/* map variable will be used to manipulate the map. This will be initialized like an OpenLayers.Map object. */
var map;
/* updateMap() allow user to modify the considered zone, in case a change has been requested (e.g. user request) */
function updateMap(coordinates) {
/* transforming given coordinates, which have to be expressed in WGS-1984 projection, to map's projection */
var position = new OpenLayers.LonLat(coordinates["lng"], coordinates["lat"]).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
/* centering in position with given zoom */
map.setCenter(position, coordinates["zoom"]);
}
/* doSearch() performs the request to geonames server with the given query, and updates the map in case of success. */
function doSearch(query) {
/* performing request with JSON API from geonames and global username (identifying request's author). Asking for only one result, to save resources and to allow the user to avoid a choice when doing the research. */
$.ajax({
url: "http://api.geonames.org/searchJSON",
dataType: 'json',
data: {
q: encodeURIComponent(query),
maxRows: "1",
username: geonamesUser
},
success: function(data) {
/* updating the map if response is ok */
if (typeof data["geonames"][0]!= 'undefined') {
var coordinates = new Array();
coordinates["lng"] = data["geonames"][0].lng;
coordinates["lat"] = data["geonames"][0].lat;
/* if position represents a country, setting the zoom to 6 */
if (data["geonames"][0].fcl == "A")
coordinates["zoom"] = 6;
else
coordinates["zoom"] = 12;
updateMap(coordinates);
}
}
});
}
/*
* doNominatim() could use Nominatim API (http://wiki.openstreetmap.org/wiki/Nominatim).
* It is not used because first result is not always pertinent and we want to avoid user to make a choice after his request.
* Bounding box info provided by Nominatim could be a good way to set the right zoom for request (instead of having a determined zoom for countries and cities).
* Nominatim allows user (the developer in this case) to set the language to display informations provided in sankoré user native language.
*/
function doNominatim(query) {
$.ajax({
url: "http://nominatim.openstreetmap.org/search",
dataType: "json",
data: {
q: query,
format: "json",
limit: "10",
addressdetails: "1"
},
success: function(data) {
/* updating the map if response is ok */
if (typeof data[0]!= 'undefined') {
var coordinates = new Array();
coordinates["lng"] = data[0].lon;
coordinates["lat"] = data[0].lat;
/* if position represents a city, setting the zoom to 12 */
if (data[0].address.state)
coordinates["zoom"] = 12;
else
coordinates["zoom"] = 6;
updateMap(coordinates);
}
}
});
}
window.onload = function() {
map = new OpenLayers.Map({
div: "map"
});
/* limiting max zoom, by overriding OpenLayers.Map::moveTo function */
map.newMoveTo = map.moveTo;
map.moveTo = function(lonlat, zoom, options) {
return(zoom >= 0 && zoom <= 17) ? map.newMoveTo(lonlat,zoom,options) : false;
};
/* Standard layer */
classic = new OpenLayers.Layer.OSM("Classic");
/* avoiding out of bounds requests */
classic.displayOutsideMaxExtent = false;
/* Landscape layer */
relief = new OpenLayers.Layer.OSM("Relief", [
'http://a.tile3.opencyclemap.org/landscape/${z}/${x}/${y}.png',
'http://b.tile3.opencyclemap.org/landscape/${z}/${x}/${y}.png',
'http://c.tile3.opencyclemap.org/landscape/${z}/${x}/${y}.png'
]);
relief.displayOutsideMaxExtent = false;
/* then adding layers to the map */
map.addLayers([classic, relief]);
/* displaying scale line, allowing user to represent distances */
map.addControl(new OpenLayers.Control.ScaleLine());
/* then zomming to get a better overview of the worldmap */
map.zoomTo(2);
/* doSearch is called when form is submitted */
$("#search_form").submit(function (event) {
doSearch($("#query").val());
return false;
});
/* allowing user to simply switch between classic and relief layers */
$("#layout_switch > a").click(function (event) {
newLayer = $(this).text() == "Relief" ? relief : classic;
newText = $(this).text() == "Relief" ? "Classic" : "Relief";
$(this).text(newText);
map.setBaseLayer(newLayer);
return false;
});
};
-->
</script>
</head>
<body>
<div id="map">
</div>
<div id="layout_switch">
<a href="#">Relief</a>
</div>
<form method="post" id="search_form" action="#" accept-charset="UTF-8">
<p>
<input type="text" value="" tabindex="1" id="query" />
<input type="submit" value="Go" />
</p>
</form>
</body>
</html>