/* 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. */
* 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 */