Advertisement
hfelix

Laravel GeoCoding Api Helper

May 5th, 2021
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.71 KB | None | 0 0
  1.   public static function googleGeocodeDistance( $user, $dados ) {
  2.         try {
  3.             if ( !isset( $user->organizacao ) ) {
  4.                 return false;
  5.             }
  6.  
  7.             $organizacao = $user->organizacao;
  8.  
  9.             $params = 'key='.$organizacao->google_server_key;
  10.             $baseUrl = 'https://maps.googleapis.com/maps/api/distancematrix/json?';
  11.             $params .= '&origins='.$dados['Origin']['lat'].','.$dados['Origin']['lon'];
  12.             $params .= '&destinations='.$dados['Destination']['lat'].','.$dados['Destination']['lon'];
  13.             $params .= '&units=metric&language=pt';
  14.             $url = $baseUrl.$params;
  15.  
  16.             $curl = curl_init();
  17.             curl_setopt_array( $curl, array(
  18.                 CURLOPT_URL => $url,
  19.                 CURLOPT_RETURNTRANSFER => true,
  20.                 CURLOPT_ENCODING => '',
  21.                 CURLOPT_MAXREDIRS => 10,
  22.                 CURLOPT_TIMEOUT => 0,
  23.                 CURLOPT_FOLLOWLOCATION => true,
  24.                 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  25.                 CURLOPT_CUSTOMREQUEST => 'GET',
  26.             ) );
  27.  
  28.             $response = json_decode( curl_exec( $curl ), true );
  29.             curl_close( $curl );
  30.             return Helper::formatarGeocode( $response );
  31.         } catch ( \Throwable $th ) {
  32.             dd( $th );
  33.         }
  34.     }
  35.  
  36.     public static function googleGeocodePlace( $user, $dados ) {
  37.         try {
  38.  
  39.             if ( !isset( $user->organizacao ) ) {
  40.                 return false;
  41.             }
  42.  
  43.             $organizacao = $user->organizacao;
  44.             $params = 'key='.$organizacao->google_server_key.'&language=pt&units=metric';
  45.             $baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json?';
  46.             $params .= '&latlng='.$dados['Origin']['lat'].','.$dados['Origin']['lon'];
  47.             $url = $baseUrl.$params;
  48.  
  49.             $curl = curl_init();
  50.             curl_setopt_array( $curl, array(
  51.                 CURLOPT_URL => $url,
  52.                 CURLOPT_RETURNTRANSFER => true,
  53.                 CURLOPT_ENCODING => '',
  54.                 CURLOPT_MAXREDIRS => 10,
  55.                 CURLOPT_TIMEOUT => 0,
  56.                 CURLOPT_FOLLOWLOCATION => true,
  57.                 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  58.                 CURLOPT_CUSTOMREQUEST => 'GET',
  59.             ) );
  60.             $response = json_decode( curl_exec( $curl ), true );
  61.             curl_close( $curl );
  62.             return $response;
  63.         } catch ( \Throwable $th ) {
  64.             dd( $th );
  65.         }
  66.     }
  67.  
  68.     public static function formatarGeocode( $response ) {
  69.         try {
  70.             if (
  71.                 isset( $response['rows'][0]['elements'][0] ) and
  72.                 isset( $response['destination_addresses'] )  and
  73.                 isset( $response['origin_addresses'] )   and
  74.                 isset( $response['destination_addresses'] )
  75.             ) {
  76.                 $el = $response['rows'][0]['elements'][0];
  77.                 $dados = [
  78.                     'error' => false,
  79.                     'origem' => $response['origin_addresses'][0],
  80.                     'destino' => $response['destination_addresses'][0],
  81.                     'freguesia_origem' => Helper::extrairConcelhoGeoCode( $response['origin_addresses'][0] ),
  82.                     'freguesia_destino' => Helper::extrairConcelhoGeoCode( $response['destination_addresses'][0] ),
  83.                     'distance' => [],
  84.                     'duration' => [],
  85.                 ];
  86.  
  87.                 if ( isset( $el['distance']['value'] ) ) {
  88.                     $dados['distance'] = [
  89.                         'valor' => $el['distance']['value'] ,
  90.                         'formatado' => $el['distance']['text'] ,
  91.                     ];
  92.                 }
  93.  
  94.                 if ( isset( $el['duration']['value'] ) ) {
  95.                     $dados['duration'] = [
  96.                         'valor' => $el['duration']['value'] ,
  97.                         'formatado' => $el['duration']['text'] ,
  98.                     ];
  99.                 }
  100.                 return $dados;
  101.  
  102.             } else {
  103.                 return [
  104.                     'error' => true,
  105.                     'message' => 'Falha ao buscar pelas coordenadas enviadas.Verifique os dados e tente novamente'
  106.                 ];
  107.             }
  108.         } catch ( \Throwable $th ) {
  109.             dd( $th );
  110.         }
  111.     }
  112.  
  113.     public static function extrairConcelhoGeoCode( $value ) {
  114.         try {
  115.  
  116.             if ( $value == null or $value == '' ) {
  117.                 return $value;
  118.             }
  119.  
  120.             $arr = explode( ',', $value );
  121.             if ( filter_var( $arr[1], FILTER_SANITIZE_NUMBER_INT )  !== '' ) {
  122.                 $concelho =  preg_replace( '/\d/', '', $arr[1] );
  123.                 $concelho = trim( str_replace( ' - ', '', $concelho ) );
  124.                 if ( $concelho == '' or $concelho == '-' ) {
  125.                     if ( filter_var( $arr[2], FILTER_SANITIZE_NUMBER_INT )  !== '' ) {
  126.                         $concelho =  preg_replace( '/\d/', '', $arr[2] );
  127.                         $concelho = trim( str_replace( ' - ', '', $concelho ) );
  128.                         return $concelho;
  129.                     }
  130.                 } else {
  131.                     return $concelho;
  132.                 }
  133.             }
  134.  
  135.         } catch ( Exception $th ) {
  136.             throw new Exception( $th, 1 );
  137.         }
  138.     }
  139.  
  140.     public static function removerAcentos( $str ) {
  141.         return preg_replace( array( '/(á|à|ã|â|ä)/', '/(Á|À|Ã|Â|Ä)/', '/(é|è|ê|ë)/', '/(É|È|Ê|Ë)/', '/(í|ì|î|ï)/', '/(Í|Ì|Î|Ï)/', '/(ó|ò|õ|ô|ö)/', '/(Ó|Ò|Õ|Ô|Ö)/', '/(ú|ù|û|ü)/', '/(Ú|Ù|Û|Ü)/', '/(ñ)/', '/(Ñ)/' ), explode( ' ', 'a A e E i I o O u U n N' ), $str );
  142.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement