diff --git a/Api/Geocoding/Nominatim.php b/Api/Geocoding/Nominatim.php new file mode 100644 index 000000000..2f5ad2d3b --- /dev/null +++ b/Api/Geocoding/Nominatim.php @@ -0,0 +1,74 @@ +setMethod(RequestMethod::GET); + + // Required according to the api documentation + $request->header->set('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1'); + + // Handling rate limit of the Api + $time = \microtime(true); + if ($time - self::$lastRun < 1000000) { + \usleep((int) (1000000 - ($time - self::$lastRun) + 100)); + } + + $body = Rest::request($request)->getBody(); + $result['body'] = $body; + + /** @var array $json */ + $json = \json_decode($body, true); + if ($json === false) { + return [ + 'lat' => 0.0, + 'lon' => 0.0, + ]; + } + + return [ + 'lat' => (float) ($json[0]['lat'] ?? 0.0), + 'lon' => (float) ($json[0]['lon'] ?? 0.0), + ]; + } +}