Php ile Konum Verileri Alma
2
●196
- 26-02-2023, 01:42:10Üyeliği durdurulduMerhaba codeigniter alt yapısıyla bir sistem geliştiriyorum. Otomatik olarak kullanıcının bulundugu konumu google verisiyle net olarak, il ilce mahalle sırasıyla nasıl çekebilirim herhangi bir kaynak bilen varmı.
- 26-02-2023, 01:56:00ipinfo.io'yu kullanabilirsiniz, aylık 50K istek ücretsiz.
https://ipinfo.io/
function get_ipinfo($ip) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_URL, 'http://ipinfo.io/' . $ip . '/json?token={tokenCode}'); $file = curl_exec($ch); if (strpos($file, 'country') !== FALSE) { return $file; } curl_close($ch); return NULL; } - 26-02-2023, 01:58:56
<!DOCTYPE html> <html> <head> <title>Konum Bilgisi Alma</title> <script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { alert("Tarayıcınız konumlandırmayı desteklemiyor."); } } function showPosition(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; document.getElementById("latitude").value = latitude; document.getElementById("longitude").value = longitude; document.getElementById("locationForm").submit(); } </script> </head> <body onload="getLocation()"> <form id="locationForm" method="post" action="process_location.php"> <input type="hidden" id="latitude" name="latitude" value=""> <input type="hidden" id="longitude" name="longitude" value=""> </form> </body> </html>function reverseGeocode($latitude, $longitude) { $url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $latitude . ',' . $longitude . '&key=YOUR_API_KEY'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true); if ($data['status'] == 'OK') { $result = array(); foreach ($data['results'][0]['address_components'] as $component) { if (in_array('locality', $component['types'])) { $result['city'] = $component['long_name']; } if (in_array('administrative_area_level_1', $component['types'])) { $result['state'] = $component['long_name']; } if (in_array('administrative_area_level_2', $component['types'])) { $result['district'] = $component['long_name']; } if (in_array('neighborhood', $component['types'])) { $result['neighborhood'] = $component['long_name']; } } return $result; } else { return false; } } // Kullanıcının konum bilgilerini alın $latitude = $_POST['latitude'];$longitude = $_POST['longitude']; // Google Maps Geocoding API'ye sorgu gönderin $locationData = reverseGeocode($latitude, $longitude); // Sonuçları kullanın if ($locationData) { $city = $locationData['city']; $state = $locationData['state']; $district = $locationData['district']; $neighborhood = $locationData['neighborhood']; }chatgpt ile yazdırdım, tek yapmanız gereken html dosyasındaki olayı direkt olarak ajax yada fetch ile php dosyasına post etmeniz ek olarak maps api keyi girmeniz yeterli