Last active
September 24, 2023 15:36
-
-
Save edyrkaj/2ac368a37019fda8452a6e1209611193 to your computer and use it in GitHub Desktop.
Google Maps API Curl Call - Laravel Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2017. Ezeclip | |
*/ | |
namespace App\Services; | |
class MapService { | |
// Define Constants | |
const GOOGLE_API_KEY = "XXX"; | |
/** | |
* @param string $address | |
* | |
* @param bool $jsonFormat | |
* | |
* @return \Illuminate\Http\JsonResponse | |
* @author Eledi Dyrkaj | |
* @company Manoolia/Digitaleheimat | |
*/ | |
public static function getLocation( $address = "", $jsonFormat = true ) { | |
$address = urlencode( $address ); | |
$key = self::GOOGLE_API_KEY; | |
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=$key"; | |
// Create a curl call | |
$ch = curl_init(); | |
$timeout = 0; | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch, CURLOPT_HEADER, 0 ); | |
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout ); | |
$data = curl_exec( $ch ); | |
// send request and wait for response | |
$response = json_decode( $data, true ); | |
curl_close( $ch ); | |
if ( $jsonFormat == true ) { | |
return response()->json( $response, 200 ); | |
} else { | |
return $response; | |
} | |
} | |
/** | |
* @param string $address | |
* | |
* @author Eledi Dyrkaj | |
* @company Manoolia/Digitaleheimat | |
* @return mixed | |
*/ | |
public static function getLatLng( $address = "" ) { | |
$geoData = self::getLocation( $address, false ); | |
if(empty($geoData['results'])) { | |
return ['lat' => null, 'lng' => null]; | |
} else { | |
return $geoData['results'][0]['geometry']['location']; | |
} | |
} | |
} |
nice! helped me alot :)
Thnx @GorianDriza
Thnxx man, saved my day
Nam fare
The mother .....
Thanks mate!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work!