[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins / gis / javascript / gis_geocoder.js
1 /*
2 * L.Geocoder is used to make geocoding or reverse geocoding requests.
3 */
4
5 L.Geocoder = L.Class.extend({
6
7 includes: L.Mixin.Events,
8
9 options: {
10 forwardUrl: L.geocoderConfig.forwardUrl,
11 reverseUrl: L.geocoderConfig.reverseUrl,
12 limit: 1,
13 acceptLanguage:'fr',
14 addressdetails: 1
15 },
16
17 initialize: function (callback, options) {
18 L.Util.setOptions(this, options);
19 this._user_callback = callback;
20 },
21
22 geocode: function (data) {
23 if (L.LatLng && (data instanceof L.LatLng)) {
24 this._reverse_geocode(data);
25 } else if (typeof(data) == 'string') {
26 this.options.search = data;
27 this._geocode(data);
28 }
29 },
30
31 _geocode: function (text) {
32 this._request(
33 this.options.forwardUrl,
34 {
35 format: 'json',
36 q: text,
37 limit: this.options.limit,
38 addressdetails: this.options.addressdetails,
39 'accept-language': this.options.acceptLanguage
40 }
41 );
42 },
43
44 _reverse_geocode: function (latlng) {
45 this._request(
46 this.options.reverseUrl,
47 {
48 format: 'json',
49 lat: latlng.lat,
50 lon: latlng.lng,
51 'accept-language': this.options.acceptLanguage
52 }
53 );
54 },
55
56 _request: function (url, data) {
57 jQuery.ajax({
58 cache: true,
59 context: this,
60 data: data,
61 success: this._callback,
62 url: url
63 });
64 },
65
66 _callback: function (response, textStatus, jqXHR) {
67 var return_location = {},
68 geocoder_server = false;
69 if (this.options.search) {
70 return_location.search = this.options.search;
71 }
72 if (response.type === 'FeatureCollection') {
73 geocoder_server = 'photon';
74 }
75 if (((response instanceof Array) && (!response.length)) || ((response instanceof Object) && (response.error))) {
76 return_location.error = 'not found';
77 } else {
78 return_location.street = return_location.postcode = return_location.postcode =
79 return_location.locality = return_location.region = return_location.country = '';
80 if (geocoder_server == 'photon') {
81 if (!response.features.length || response.features.length == 0) {
82 return_location.error = 'not found';
83 }
84 else {
85 place = response.features[0];
86 var street_components = [];
87
88 if (place.properties.country) {
89 return_location.country = place.properties.country;
90 }
91 if (place.properties.country_code) {
92 return_location.country_code = place.properties.country_code;
93 }
94 if (place.properties.state) {
95 return_location.region = place.properties.state;
96 }
97 if (place.properties.city) {
98 return_location.locality = place.properties.city;
99 } else if (place.properties.town) {
100 return_location.locality = place.properties.town;
101 } else if (place.properties.village) {
102 return_location.locality = place.properties.village;
103 } else if (place.properties.osm_key == 'place' && (place.properties.osm_value == 'city' || place.properties.osm_value == 'village')) {
104 return_location.locality = place.properties.name;
105 } else if (place.properties.county) {
106 street_components.push(place.properties.county);
107 }
108 if (place.properties.postcode) {
109 return_location.postcode = place.properties.postcode;
110 }
111 if (place.properties.street) {
112 street_components.push(place.properties.street);
113 } else if (place.properties.road) {
114 street_components.push(place.properties.road);
115 } else if (place.properties.pedestrian) {
116 street_components.push(place.properties.pedestrian);
117 }
118 if (place.properties.housenumber) {
119 street_components.unshift(place.properties.housenumber);
120 }
121 if (return_location.street === '' && street_components.length > 0) {
122 return_location.street = street_components.join(' ');
123 }
124 place.lat = place.geometry.coordinates[1];
125 place.lon = place.geometry.coordinates[0];
126 return_location.point = new L.LatLng(place.lat, place.lon);
127 }
128 }
129 else {
130 if (response.length > 0)
131 place = response[0];
132 else {
133 place = response;
134 }
135
136 var street_components = [];
137
138 if (place.address.country) {
139 return_location.country = place.address.country;
140 }
141 if (place.address.country_code) {
142 return_location.country_code = place.address.country_code;
143 }
144 if (place.address.state) {
145 return_location.region = place.address.state;
146 }
147 if (place.address.city) {
148 return_location.locality = place.address.city;
149 } else if (place.address.town) {
150 return_location.locality = place.address.town;
151 } else if (place.address.village) {
152 return_location.locality = place.address.village;
153 } else if (place.address.county) {
154 street_components.push(place.address.county);
155 }
156 if (place.address.postcode) {
157 return_location.postcode = place.address.postcode;
158 }
159 if (place.address.road) {
160 street_components.push(place.address.road);
161 } else if (place.address.pedestrian) {
162 street_components.push(place.address.pedestrian);
163 }
164 if (place.address.house_number) {
165 street_components.unshift(place.address.house_number);
166 }
167 if (return_location.street === '' && street_components.length > 0) {
168 return_location.street = street_components.join(' ');
169 }
170 return_location.point = new L.LatLng(place.lat, place.lon);
171 }
172 }
173 this.fire('complete', {result: return_location});
174 this._user_callback(return_location);
175 }
176 });