Setting::getValue('geolocation.enabled', false), 'provider' => Setting::getValue('geolocation.provider', 'locationiq'), 'api_keys' => json_decode(Setting::getValue('geolocation.api_keys', '{}'), true), 'language' => Setting::getValue('geolocation.language', 'es'), 'country_bias' => Setting::getValue('geolocation.country_bias', 'MX'), ]; } /** * Obtiene coordenadas a partir de una dirección. */ public function getCoordinates(string $address, ?string $provider = null) { $config = $this->getConfig(); if (!$config['enabled']) { return null; } $provider = $provider ?: $config['provider']; $apiKey = $config['api_keys'][$provider] ?? null; if (!$apiKey) { return null; } return match ($provider) { 'locationiq' => $this->getLocationIQCoordinates($address, $apiKey, $config), 'google' => $this->getGoogleCoordinates($address, $apiKey, $config), default => null, }; } /** * Llama a la API de LocationIQ para obtener coordenadas. */ protected function getLocationIQCoordinates(string $address, string $apiKey, array $config) { $url = "https://us1.locationiq.com/v1/search.php"; $response = Http::get($url, [ 'key' => $apiKey, 'q' => $address, 'format' => 'json', 'accept-language' => $config['language'], 'countrycodes' => $config['country_bias'] ]); if ($response->successful()) { $data = $response->json(); return [ 'lat' => $data[0]['lat'] ?? null, 'lng' => $data[0]['lon'] ?? null, ]; } return null; } /** * Llama a la API de Google Maps para obtener coordenadas. */ protected function getGoogleCoordinates(string $address, string $apiKey, array $config) { $url = "https://maps.googleapis.com/maps/api/geocode/json"; $response = Http::get($url, [ 'key' => $apiKey, 'address' => $address, 'language' => $config['language'], 'region' => $config['country_bias'] ]); if ($response->successful()) { $data = $response->json(); return [ 'lat' => $data['results'][0]['geometry']['location']['lat'] ?? null, 'lng' => $data['results'][0]['geometry']['location']['lng'] ?? null, ]; } return null; } }