Skip to content

Instantly share code, notes, and snippets.

@edyrkaj
Last active September 24, 2023 15:36
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save edyrkaj/2ac368a37019fda8452a6e1209611193 to your computer and use it in GitHub Desktop.
Save edyrkaj/2ac368a37019fda8452a6e1209611193 to your computer and use it in GitHub Desktop.
Google Maps API Curl Call - Laravel Service
<?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'];
}
}
}
@GorianDriza
Copy link

Nice work!

@krisidmisso
Copy link

nice! helped me alot :)

@edyrkaj
Copy link
Author

edyrkaj commented May 9, 2017

@ilirhushi
Copy link

Thnxx man, saved my day

@BrunoKarriqi
Copy link

Nam fare

@redilinxa
Copy link

The mother .....

@NOTtheKRish
Copy link

Thanks mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment