You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

  1. <?php
  2. $app->get('/api/geocode', function() use($app) {
  3. $params = $app->request()->params();
  4. if(
  5. (k($params, 'latitude') !== null && k($params, 'longitude') !== null)
  6. || k($params, 'input') !== null
  7. ) {
  8. $response = [
  9. 'latitude' => null,
  10. 'longitude' => null,
  11. 'locality' => null,
  12. 'region' => null,
  13. 'country' => null,
  14. 'best_name' => null,
  15. 'full_name' => null,
  16. 'timezone' => null,
  17. 'offset' => null,
  18. 'seconds' => null,
  19. 'localtime' => null,
  20. ];
  21. if(k($params, 'input')) {
  22. $adr = p3k\Geocoder::geocode($params['input']);
  23. } else {
  24. $lat = (float)$params['latitude'];
  25. $lng = (float)$params['longitude'];
  26. $response['latitude'] = $lat;
  27. $response['longitude'] = $lng;
  28. $adr = p3k\Geocoder::adrFromLocation($lat, $lng);
  29. }
  30. if($adr) {
  31. $response['latitude'] = $adr->latitude;
  32. $response['longitude'] = $adr->longitude;
  33. $response['locality'] = $adr->localityName;
  34. $response['region'] = $adr->regionName;
  35. $response['country'] = $adr->countryName;
  36. $response['best_name'] = $adr->bestName;
  37. $response['full_name'] = $adr->fullName;
  38. }
  39. $timezone = p3k\Timezone::timezone_for_location($response['latitude'], $response['longitude']);
  40. if($timezone) {
  41. $response['timezone'] = $timezone->name;
  42. $response['offset'] = $timezone->offset;
  43. $response['seconds'] = $timezone->seconds;
  44. $response['localtime'] = $timezone->localtime;
  45. }
  46. json_response($app, $response);
  47. } else {
  48. json_response($app, [
  49. 'error' => 'invalid_request',
  50. 'error_description' => 'Request was missing parameters'
  51. ], 400);
  52. }
  53. });