Skip to content

Commit 2b43e1e

Browse files
authored
Merge pull request #1465 from tintou/tintou/structured-address
AddressService: Use the structured query when possible
2 parents 35a19b2 + f131c4c commit 2b43e1e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/Service/AddressService.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,28 @@ private function lookupAddressExternal($adr): array {
181181

182182
// we get rid of "post office box" field
183183
$splitted_adr = explode(';', $adr);
184-
if (count($splitted_adr) > 2) {
185-
array_shift($splitted_adr);
186-
}
187-
188184
// remove blank lines (#706)
189185
$splitted_adr = array_filter(array_map('trim', $splitted_adr));
190-
$query_adr = implode(', ', $splitted_adr);
186+
// ADR in VCard is mandated to 7 fields
187+
if (sizeof($splitted_adr) == 7) {
188+
$query_adr_parts = [];
189+
// This matches the nominatim query with the fields of 'ADR' in VCard
190+
$query_key_part = ['','','street', 'city','state', 'postalcode', 'country'];
191+
foreach ($query_key_part as $index => $query_key) {
192+
if ($query_key !== '' && $splitted_adr[$index] !== '') {
193+
$query_adr_parts += $query_key . '=' . urlencode($splitted_adr[$index]);
194+
}
195+
}
196+
197+
$query_adr = implode(';', $query_adr_parts);
198+
} else {
199+
// Try to do our best with a naive query
200+
$query_adr = 'q=' . urlencode(implode(', ', $splitted_adr));
201+
}
202+
191203

192204
$result_json = @file_get_contents(
193-
'https://nominatim.openstreetmap.org/search.php?q=' . urlencode($query_adr) . '&format=jsonv2',
205+
'https://nominatim.openstreetmap.org/search?format=jsonv2&' . $query_adr,
194206
false,
195207
$context
196208
);

0 commit comments

Comments
 (0)