initial
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Google Maps helper.
|
||||
* All Google Maps API calls from the backend should go through here so that
|
||||
* the API key lives only on the server (via constants configured in setting.php).
|
||||
*/
|
||||
|
||||
if (!function_exists('maps_get_api_key')) {
|
||||
/**
|
||||
* Resolve the Maps API key from environment/constant.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function maps_get_api_key()
|
||||
{
|
||||
if (defined('google_maps_api') && google_maps_api !== '') {
|
||||
return google_maps_api;
|
||||
}
|
||||
if (defined('GOOGLE_MAPS_API_KEY') && GOOGLE_MAPS_API_KEY !== '') {
|
||||
return GOOGLE_MAPS_API_KEY;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('maps_http_get_json')) {
|
||||
/**
|
||||
* Internal helper to issue a GET request and decode JSON.
|
||||
*
|
||||
* @param string $url
|
||||
* @return array|null
|
||||
*/
|
||||
function maps_http_get_json($url)
|
||||
{
|
||||
$ctx = stream_context_create(array(
|
||||
'http' => array(
|
||||
'method' => 'GET',
|
||||
'timeout' => 10,
|
||||
),
|
||||
));
|
||||
$raw = @file_get_contents($url, false, $ctx);
|
||||
if ($raw === false) {
|
||||
log_message('error', 'Maps helper: HTTP request failed for URL: ' . $url);
|
||||
return null;
|
||||
}
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data)) {
|
||||
log_message('error', 'Maps helper: JSON decode failed for URL: ' . $url);
|
||||
return null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('maps_directions')) {
|
||||
/**
|
||||
* Call Google Directions API for a simple origin/destination pair.
|
||||
*
|
||||
* @param float $originLat
|
||||
* @param float $originLng
|
||||
* @param float $destLat
|
||||
* @param float $destLng
|
||||
* @param string $mode
|
||||
* @return array|null
|
||||
*/
|
||||
function maps_directions($originLat, $originLng, $destLat, $destLng, $mode = 'driving')
|
||||
{
|
||||
$key = maps_get_api_key();
|
||||
if ($key === '') {
|
||||
log_message('error', 'Maps helper: GOOGLE_MAPS_API_KEY not set');
|
||||
return null;
|
||||
}
|
||||
$params = http_build_query(array(
|
||||
'origin' => $originLat . ',' . $originLng,
|
||||
'destination' => $destLat . ',' . $destLng,
|
||||
'mode' => $mode,
|
||||
'units' => 'metric',
|
||||
'key' => $key,
|
||||
));
|
||||
$url = 'https://maps.googleapis.com/maps/api/directions/json?' . $params;
|
||||
return maps_http_get_json($url);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('maps_geocode')) {
|
||||
/**
|
||||
* Reverse geocode a lat/lng using Google Geocoding API.
|
||||
*
|
||||
* @param float $lat
|
||||
* @param float $lng
|
||||
* @return array|null
|
||||
*/
|
||||
function maps_geocode($lat, $lng)
|
||||
{
|
||||
$key = maps_get_api_key();
|
||||
if ($key === '') {
|
||||
log_message('error', 'Maps helper: GOOGLE_MAPS_API_KEY not set for geocode');
|
||||
return null;
|
||||
}
|
||||
$params = http_build_query(array(
|
||||
'latlng' => $lat . ',' . $lng,
|
||||
'key' => $key,
|
||||
));
|
||||
$url = 'https://maps.googleapis.com/maps/api/geocode/json?' . $params;
|
||||
return maps_http_get_json($url);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user