[PLUGINS] +set de base
[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: 'http://open.mapquestapi.com/nominatim/v1/search',
11 reverseUrl: 'http://open.mapquestapi.com/nominatim/v1/reverse',
12 limit: 1,
13 addressdetails: 1
14 },
15
16 initialize: function (callback, options) {
17 L.Util.setOptions(this, options);
18 this._user_callback = callback;
19 },
20
21 geocode: function (data) {
22 if (L.LatLng && (data instanceof L.LatLng)) {
23 this._reverse_geocode(data);
24 } else if (typeof(data) == 'string') {
25 this._geocode(data);
26 }
27 },
28
29 _geocode: function (text) {
30 this._request(
31 this.options.forwardUrl,
32 {
33 format: 'json',
34 q: text,
35 limit: this.options.limit,
36 addressdetails: this.options.addressdetails
37 }
38 );
39 },
40
41 _reverse_geocode: function (latlng) {
42 this._request(
43 this.options.reverseUrl,
44 {
45 format: 'json',
46 lat: latlng.lat,
47 lon: latlng.lng
48 }
49 );
50 },
51
52 _request: function (url, data) {
53 $.ajax({
54 cache: true,
55 context: this,
56 data: data,
57 dataType: 'jsonp',
58 jsonp: 'json_callback',
59 success: this._callback,
60 url: url
61 });
62 },
63
64 _callback: function (response) {
65 var return_location = {};
66 if (response instanceof Array && !response.length) {
67 return false;
68 } else {
69 return_location.street = '';
70 return_location.postcode = '';
71 return_location.locality = '';
72 return_location.region = '';
73 return_location.country = '';
74
75 if (response.length > 0) {
76 place = response[0];
77 } else {
78 place = response;
79 }
80
81 var street_components = [];
82
83 if (place.address.country) {
84 return_location.country = place.address.country;
85 }
86 if (place.address.state) {
87 return_location.region = place.address.state;
88 }
89 if (place.address.city) {
90 return_location.locality = place.address.city;
91 }else if(place.address.county){
92 street_components.push(place.address.pedestrian);
93 }
94 if (place.address.postcode) {
95 return_location.postcode = place.address.postcode;
96 }
97 if (place.address.road) {
98 street_components.push(place.address.road);
99 }else if(place.address.pedestrian){
100 street_components.push(place.address.pedestrian);
101 }
102 if (place.address.house_number) {
103 street_components.unshift(place.address.house_number);
104 }
105
106 if (return_location.street === '' && street_components.length > 0) {
107 return_location.street = street_components.join(' ');
108 }
109
110 return_location.point = new L.LatLng(place.lat, place.lon);
111
112 this._user_callback(return_location);
113 }
114 }
115 });