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); } }