Как найти полный адрес пользователя

Я пытаюсь найти полный адрес пользователя с помощью API Geoplugin, но он дает мне страну местоположения вместо полного адреса, когда я выполняю адрес. Я хочу получить точное местоположение пользователя, поскольку я использую его для построения начальной точки на карте Google. Мой код ниже

function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
$output = NULL;
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
}
$purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
$support    = array("country", "countrycode", "state", "region", "city", "location", "address");
$continents = array(
"AF" => "Africa",
"AN" => "Antarctica",
"AS" => "Asia",
"EU" => "Europe",
"OC" => "Australia (Oceania)",
"NA" => "North America",
"SA" => "South America");
if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
switch ($purpose) {
case "location":
$output = array(
"city"           => @$ipdat->geoplugin_city,
"state"          => @$ipdat->geoplugin_regionName,
"country"        => @$ipdat->geoplugin_countryName,
"country_code"   => @$ipdat->geoplugin_countryCode,
"continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
"continent_code" => @$ipdat->geoplugin_continentCode
);
break;
case "address":
$address = array($ipdat->geoplugin_countryName);
if (@strlen($ipdat->geoplugin_regionName) >= 1)
$address[] = $ipdat->geoplugin_regionName;
if (@strlen($ipdat->geoplugin_city) >= 1)
$address[] = $ipdat->geoplugin_city;
$output = implode(", ", array_reverse($address));
break;
case "city":
$output = @$ipdat->geoplugin_city;
break;
case "state":
$output = @$ipdat->geoplugin_regionName;
break;
case "region":
$output = @$ipdat->geoplugin_regionName;
break;
case "country":
$output = @$ipdat->geoplugin_countryName;
break;
case "countrycode":
$output = @$ipdat->geoplugin_countryCode;
break;
}
}
}
return $output;
}

echo = ip_info("Visitor", "address");

0

Решение

Вы не можете найти точный адрес пользователя с помощью IP. Но я могу сказать, как вы можете найти это, не спрашивая их. Это не незаконно, потому что ваш код будет запрашивать разрешение у них.
Вы не можете сделать это, просто используя PHP, вам также понадобится код JavaScript.

Давайте проверим обнаружение адреса с помощью Google Maps API и Javascript. Для получения дополнительной информации ищите его. Или просто использовать Документация Google MAPS API для JavaScript

geocoordinates.php

<?php

if(isset($_POST['lat'], $_POST['lng'])) {
$lat = $_POST['lat'];
$lng = $_POST['lng'];

$url = sprintf("https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s", $lat, $lng);

$content = file_get_contents($url); // get json content

$metadata = json_decode($content, true); //json decoder

if(count($metadata['results']) > 0) {
// for format example look at url
// https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452
$result = $metadata['results'][0];

// save it in db for further use
echo $result['formatted_address'];

}
else {
// no results returned
}
}

?>

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geocoding Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(savePosition, positionError, {timeout:10000});
} else {
//Geolocation is not supported by this browser
}
}

// handle the error here
function positionError(error) {
var errorCode = error.code;
var message = error.message;

alert(message);
}

function savePosition(position) {
$.post("geocoordinates.php", {lat: position.coords.latitude, lng: position.coords.longitude});
}
</script>
</head>
<body>
<button onclick="getLocation();">Get My Location</button>
</body>
</html>
0

Другие решения

Других решений пока нет …