Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function googleGeocodeDistance( $user, $dados ) {
- try {
- if ( !isset( $user->organizacao ) ) {
- return false;
- }
- $organizacao = $user->organizacao;
- $params = 'key='.$organizacao->google_server_key;
- $baseUrl = 'https://maps.googleapis.com/maps/api/distancematrix/json?';
- $params .= '&origins='.$dados['Origin']['lat'].','.$dados['Origin']['lon'];
- $params .= '&destinations='.$dados['Destination']['lat'].','.$dados['Destination']['lon'];
- $params .= '&units=metric&language=pt';
- $url = $baseUrl.$params;
- $curl = curl_init();
- curl_setopt_array( $curl, array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 0,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => 'GET',
- ) );
- $response = json_decode( curl_exec( $curl ), true );
- curl_close( $curl );
- return Helper::formatarGeocode( $response );
- } catch ( \Throwable $th ) {
- dd( $th );
- }
- }
- public static function googleGeocodePlace( $user, $dados ) {
- try {
- if ( !isset( $user->organizacao ) ) {
- return false;
- }
- $organizacao = $user->organizacao;
- $params = 'key='.$organizacao->google_server_key.'&language=pt&units=metric';
- $baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json?';
- $params .= '&latlng='.$dados['Origin']['lat'].','.$dados['Origin']['lon'];
- $url = $baseUrl.$params;
- $curl = curl_init();
- curl_setopt_array( $curl, array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 0,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => 'GET',
- ) );
- $response = json_decode( curl_exec( $curl ), true );
- curl_close( $curl );
- return $response;
- } catch ( \Throwable $th ) {
- dd( $th );
- }
- }
- public static function formatarGeocode( $response ) {
- try {
- if (
- isset( $response['rows'][0]['elements'][0] ) and
- isset( $response['destination_addresses'] ) and
- isset( $response['origin_addresses'] ) and
- isset( $response['destination_addresses'] )
- ) {
- $el = $response['rows'][0]['elements'][0];
- $dados = [
- 'error' => false,
- 'origem' => $response['origin_addresses'][0],
- 'destino' => $response['destination_addresses'][0],
- 'freguesia_origem' => Helper::extrairConcelhoGeoCode( $response['origin_addresses'][0] ),
- 'freguesia_destino' => Helper::extrairConcelhoGeoCode( $response['destination_addresses'][0] ),
- 'distance' => [],
- 'duration' => [],
- ];
- if ( isset( $el['distance']['value'] ) ) {
- $dados['distance'] = [
- 'valor' => $el['distance']['value'] ,
- 'formatado' => $el['distance']['text'] ,
- ];
- }
- if ( isset( $el['duration']['value'] ) ) {
- $dados['duration'] = [
- 'valor' => $el['duration']['value'] ,
- 'formatado' => $el['duration']['text'] ,
- ];
- }
- return $dados;
- } else {
- return [
- 'error' => true,
- 'message' => 'Falha ao buscar pelas coordenadas enviadas.Verifique os dados e tente novamente'
- ];
- }
- } catch ( \Throwable $th ) {
- dd( $th );
- }
- }
- public static function extrairConcelhoGeoCode( $value ) {
- try {
- if ( $value == null or $value == '' ) {
- return $value;
- }
- $arr = explode( ',', $value );
- if ( filter_var( $arr[1], FILTER_SANITIZE_NUMBER_INT ) !== '' ) {
- $concelho = preg_replace( '/\d/', '', $arr[1] );
- $concelho = trim( str_replace( ' - ', '', $concelho ) );
- if ( $concelho == '' or $concelho == '-' ) {
- if ( filter_var( $arr[2], FILTER_SANITIZE_NUMBER_INT ) !== '' ) {
- $concelho = preg_replace( '/\d/', '', $arr[2] );
- $concelho = trim( str_replace( ' - ', '', $concelho ) );
- return $concelho;
- }
- } else {
- return $concelho;
- }
- }
- } catch ( Exception $th ) {
- throw new Exception( $th, 1 );
- }
- }
- public static function removerAcentos( $str ) {
- return preg_replace( array( '/(á|à|ã|â|ä)/', '/(Á|À|Ã|Â|Ä)/', '/(é|è|ê|ë)/', '/(É|È|Ê|Ë)/', '/(í|ì|î|ï)/', '/(Í|Ì|Î|Ï)/', '/(ó|ò|õ|ô|ö)/', '/(Ó|Ò|Õ|Ô|Ö)/', '/(ú|ù|û|ü)/', '/(Ú|Ù|Û|Ü)/', '/(ñ)/', '/(Ñ)/' ), explode( ' ', 'a A e E i I o O u U n N' ), $str );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement