Skip to content

Instantly share code, notes, and snippets.

@sumonst21
Created April 2, 2023 23:36
Show Gist options
  • Save sumonst21/6bd5563bb89c97817bdb3e076c87a605 to your computer and use it in GitHub Desktop.
Save sumonst21/6bd5563bb89c97817bdb3e076c87a605 to your computer and use it in GitHub Desktop.
osm reverse geocoding
<?php
/**
* osm reverse geocoding
* https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=52.5487429714954&lon=-1.81602098644987&zoom=18&addressdetails=1
*
* @param string|float $lat
* @param string|float $long
* @return array
* @throws Exception
* @see https://wiki.openstreetmap.org/wiki/Nominatim
* @see https://nominatim.org/release-docs/develop/api/Reverse/
* @author sumonst21
*/
function osm_reverse_geocoding($lat, $long)
{
$lat = trim($lat);
$long = trim($long);
$email = '';
$params = array(
'format' => 'jsonv2',
'addressdetails' => 1,
'email' => $email,
'lat' => $lat,
'lon' => $long,
'limit' => 1,
'accept-language' => 'de,en',
'dedupe' => 1,
);
$url = "https://nominatim.openstreetmap.org/reverse?" . http_build_query($params);
$res = _curl($url);
if (empty($res)) {
throw new Exception(Yii::t("default", "reverse geocoding failed"));
}
$data = json_decode($res, true);
if (empty($data) || !is_array($data) || count($data) == 0) {
throw new Exception(Yii::t("default", "reverse geocoding failed"));
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment