-
Notifications
You must be signed in to change notification settings - Fork 4
/
Control.Geocoder.ptv.js
141 lines (124 loc) · 4.29 KB
/
Control.Geocoder.ptv.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
if(!L.Control.Geocoder)
L.Control.Geocoder = {};
L.Control.Geocoder.Ptv = L.Class.extend({
options: {
serviceUrl: 'https://xlocate-eu-n-test.cloud.ptvgroup.com/xlocate/rs/XLocate/',
token: '',
fixedCountry: ''
},
initialize: function (options) {
L.Util.setOptions(this, options);
},
_buildAddressString: function (address) {
var street = (address.street + ' ' + address.houseNumber).trim();
var city = (address.postCode + ' ' + address.city).trim();
city = (address.state + ' ' + city).trim();
if (!this.options.fixedCountry)
city = (address.country + ' ' + city).trim();
if (!street)
return city;
else if (!city)
return street;
else
return street + ', ' + city;
},
// using standard xLocate geocoding as suggest/autocompletion
suggest: function(query, cb, context) {
return this.geocode(query, cb, context);
},
geocode: function (query, cb, context) {
var coord = query.match(new RegExp('^\\s*([-+]?\\d*\\.?\\d*)\\s*,\\s*([-+]?\\d*\\.?\\d*)\\s*$'));
if(coord) {
var loc = L.latLng(coord[1],coord[2]);
cb.call(context, [{
name: query,
center: loc,
bbox: L.latLngBounds(loc, loc)
}]);
return;
}
var that = this;
var url = this.options.serviceUrl + 'findAddressByText';
// sample PTV xLocate request
var request = {
'address': query,
'country': this.options.fixedCountry,
'options': [],
'sorting': [],
'additionalFields': [],
'callerContext': {
'properties': [{
'key': 'CoordFormat',
'value': 'OG_GEODECIMAL'
}, {
'key': 'Profile',
'value': 'default'
}]
}
};
runRequest(url, request, this.options.token,
function (response) {
var results = [];
for (var i = response.resultList.length - 1; i >= 0; i--) {
var resultAddress = response.resultList[i];
var loc = L.latLng(resultAddress.coordinates.point.y, resultAddress.coordinates.point.x);
results[i] = {
name: that._buildAddressString(resultAddress),
center: loc,
bbox: L.latLngBounds(loc, loc)
};
}
cb.call(context, results);
},
function (xhr) {
console.log(xhr);
});
},
reverse: function (location, scale, cb, context) {
var that = this;
var url = this.options.serviceUrl + 'findLocation';
// sample PTV xLocate request
var request = {
'location': {
'coordinate': {
'point': {
'x': location.lng,
'y': location.lat
}
}
},
'options': [],
'sorting': [],
'additionalFields': [],
'callerContext': {
'properties': [{
'key': 'CoordFormat',
'value': 'OG_GEODECIMAL'
},
{
'key': 'Profile',
'value': 'default'
}
]
}
};
runRequest(url, request, this.options.token,
function (response) {
if (response.resultList.length == 0)
return;
var resultAddress = response.resultList[0];
var loc = L.latLng(resultAddress.coordinates.point.y, resultAddress.coordinates.point.x);
cb.call(context, [{
name: that._buildAddressString(resultAddress),
center: loc,
bounds: L.latLngBounds(loc, loc)
}]);
},
function (xhr) {
console.log(xhr);
});
}
});
L.Control.Geocoder.ptv = function (options) {
return new L.Control.Geocoder.Ptv(options);
};