forked from mapstraction/mxn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxn.openlayers.geocoder.js
More file actions
95 lines (82 loc) · 2.55 KB
/
mxn.openlayers.geocoder.js
File metadata and controls
95 lines (82 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
mxn.register('openlayers', {
Geocoder: {
init: function() {
var me = this;
},
geocode: function(address){
var me = this;
if (!address.hasOwnProperty('address') || address.address === null || address.address === '') {
address.address = [ address.street, address.locality, address.region, address.country ].join(', ');
}
if (address.hasOwnProperty('lat') && address.hasOwnProperty('lon')) {
var latlon = address.toProprietary(this.api);
OpenLayers.Request.GET({
url: 'http://nominatim.openstreetmap.org/reverse',
params: {
'lat': address.lat,
'lon': address.lon,
'addressdetails': 1,
'format': 'json'
},
callback: function(request) { me.geocode_callback(JSON.parse(request.responseText), request.status); }
});
} else {
OpenLayers.Request.GET({
url: 'http://nominatim.openstreetmap.org/search',
params: {
'q': address.address,
'addressdetails': 1,
'format': 'json'
},
callback: function(request) { me.geocode_callback(JSON.parse(request.responseText), request.status); }
});
}
},
geocode_callback: function(results, status){
var return_location = {};
if (status != 200) {
this.error_callback(response.statusText);
}
else if (results instanceof Array && !results.length){
this.error_callback("OpenLayers didn't recognize this address.");
}
else {
return_location.street = '';
return_location.locality = '';
return_location.postcode = '';
return_location.region = '';
return_location.country = '';
var place;
if (results.length > 0) {
place = results[0];
} else {
place = results;
}
var street_components = [];
if (place.address.country) {
return_location.country = place.address.country;
}
if (place.address.state) {
return_location.region = place.address.state;
}
if (place.address.city) {
return_location.locality = place.address.city;
}
if (place.address.postcode) {
return_location.postcode = place.address.postcode;
}
if (place.address.road) {
street_components.push(place.address.road);
}
if (place.address.house_number) {
street_components.unshift(place.address.house_number);
}
if (return_location.street === '' && street_components.length > 0) {
return_location.street = street_components.join(' ');
}
return_location.point = new mxn.LatLonPoint(parseFloat(place.lat), parseFloat(place.lon));
this.callback(return_location);
}
}
}
});