3eec60dfeaeaeb1b3e56c2a1fdeca4de6196efce
[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 }
52 );
53 },
54
55 _request: function (url, data) {
56 jQuery.ajax({
57 cache: true,
58 context: this,
59 data: data,
60 dataType: 'jsonp',
61 jsonp: 'json_callback',
62 success: this._callback,
63 error: function (e) {
64 /**
65 * Photon me renvoie une erreur à chaque fois
66 */
67 if (e.statusText == 'OK' && e.status == '200') {
68 this._callback(e.responseText,e.status,e);
69 }
70 },
71 url: url
72 });
73 },
74
75 _callback: function (response, textStatus, jqXHR) {
76 var return_location = {},
77 geocoder_server = false;
78 if (this.options.search) {
79 return_location.search = this.options.search;
80 }
81 if (typeof response === 'string') {
82 geocoder_server = 'photon';
83 response = JSON.parse(response);
84 }
85 if (((response instanceof Array) && (!response.length)) || ((response instanceof Object) && (response.error))) {
86 return_location.error = 'not found';
87 } else {
88 return_location.street = return_location.postcode = return_location.postcode =
89 return_location.locality = return_location.region = return_location.country = '';
90 if (geocoder_server == 'photon') {
91 if (!response.features.length || response.features.length == 0) {
92 return_location.error = 'not found';
93 }
94 else {
95 place = response.features[0];
96 var street_components = [];
97
98 if (place.properties.country) {
99 return_location.country = place.properties.country;
100 }
101 if (place.properties.country_code) {
102 return_location.country_code = place.properties.country_code;
103 }
104 if (place.properties.state) {
105 return_location.region = place.properties.state;
106 }
107 if (place.properties.city) {
108 return_location.locality = place.properties.city;
109 } else if (place.properties.town) {
110 return_location.locality = place.properties.town;
111 } else if (place.properties.village) {
112 return_location.locality = place.properties.village;
113 } else if (place.properties.osm_key == 'place' && (place.properties.osm_value == 'city' || place.properties.osm_value == 'village')) {
114 return_location.locality = place.properties.name;
115 } else if (place.properties.county) {
116 street_components.push(place.properties.county);
117 }
118 if (place.properties.postcode) {
119 return_location.postcode = place.properties.postcode;
120 }
121 if (place.properties.street) {
122 street_components.push(place.properties.street);
123 }
124 else if (place.properties.road) {
125 street_components.push(place.properties.road);
126 } else if (place.properties.pedestrian) {
127 street_components.push(place.properties.pedestrian);
128 }
129 if (place.properties.housenumber) {
130 street_components.unshift(place.properties.housenumber);
131 }
132 if (return_location.street === '' && street_components.length > 0) {
133 return_location.street = street_components.join(' ');
134 }
135 place.lat = place.geometry.coordinates[1];
136 place.lon = place.geometry.coordinates[0];
137 return_location.point = new L.LatLng(place.lat, place.lon);
138 }
139 }
140 else {
141 if (response.length > 0)
142 place = response[0];
143 else {
144 place = response;
145 }
146
147 var street_components = [];
148
149 if (place.address.country) {
150 return_location.country = place.address.country;
151 }
152 if (place.address.country_code) {
153 return_location.country_code = place.address.country_code;
154 }
155 if (place.address.state) {
156 return_location.region = place.address.state;
157 }
158 if (place.address.city) {
159 return_location.locality = place.address.city;
160 } else if (place.address.town) {
161 return_location.locality = place.address.town;
162 } else if (place.address.village) {
163 return_location.locality = place.address.village;
164 } else if (place.address.county) {
165 street_components.push(place.address.county);
166 }
167 if (place.address.postcode) {
168 return_location.postcode = place.address.postcode;
169 }
170 if (place.address.road) {
171 street_components.push(place.address.road);
172 } else if (place.address.pedestrian) {
173 street_components.push(place.address.pedestrian);
174 }
175 if (place.address.house_number) {
176 street_components.unshift(place.address.house_number);
177 }
178 if (return_location.street === '' && street_components.length > 0) {
179 return_location.street = street_components.join(' ');
180 }
181 return_location.point = new L.LatLng(place.lat, place.lon);
182 }
183 }
184 this._user_callback(return_location);
185 }
186 });