[PLUGINS] ~maj gis v4.26.1-->4.26.10
[lhc/web/clavette_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 url: url
64 });
65 },
66
67 _callback: function (response,textStatus,jqXHR) {
68 var return_location = {};
69 if(this.options.search)
70 return_location.search = this.options.search;
71 if (response instanceof Array && !response.length) {
72 return_location.error = 'not found';
73 } else {
74 return_location.street = return_location.postcode = return_location.postcode =
75 return_location.locality = return_location.region = return_location.country = '';
76
77 if (response.length > 0)
78 place = response[0];
79 else {
80 place = response;
81 }
82
83 var street_components = [];
84
85 if (place.address.country) {
86 return_location.country = place.address.country;
87 }
88 if (place.address.country_code) {
89 return_location.country_code = place.address.country_code;
90 }
91 if (place.address.state) {
92 return_location.region = place.address.state;
93 }
94 //un jour peut-être…
95 /*
96 if (place.address.county) {
97 return_location.departement = place.address.county;
98 }
99 */
100 if (place.address.city) {
101 return_location.locality = place.address.city;
102 } else if (place.address.town) {
103 return_location.locality = place.address.town;
104 } else if (place.address.village) {
105 return_location.locality = place.address.village;
106 } else if (place.address.county) {
107 street_components.push(place.address.county);
108 }
109 if (place.address.postcode) {
110 return_location.postcode = place.address.postcode;
111 }
112 if (place.address.road) {
113 street_components.push(place.address.road);
114 }else if(place.address.pedestrian){
115 street_components.push(place.address.pedestrian);
116 }
117 if (place.address.house_number) {
118 street_components.unshift(place.address.house_number);
119 }
120
121 if (return_location.street === '' && street_components.length > 0) {
122 return_location.street = street_components.join(' ');
123 }
124
125 return_location.point = new L.LatLng(place.lat, place.lon);
126 }
127 this._user_callback(return_location);
128 }
129 });