我想獲取我的網頁上客戶的位置。如何獲取位置--php
我可以用PHP或Javascript來做到這一點。
我目前正試圖通過客戶端的IP地址和PHP的geoip擴展來獲取位置。
但它需要一個數據庫,我不希望這樣。
有沒有其他方法可以找到客戶的位置?
我想獲取我的網頁上客戶的位置。如何獲取位置--php
我可以用PHP或Javascript來做到這一點。
我目前正試圖通過客戶端的IP地址和PHP的geoip擴展來獲取位置。
但它需要一個數據庫,我不希望這樣。
有沒有其他方法可以找到客戶的位置?
您可以使用http://ipinfo.io/這是一個第三方數據庫。
該數據庫不需要任何插件,因此很容易在PHP中使用。
$ip = $_SERVER['REMOTE_ADDR']; // get client's IP
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));// Send to ipinfo
echo $details->city; // Gives you the city of the client.
echo $details->country; // Gives you the country of the client.
編輯:我還看到你添加了一個JavaScript標記,你可以用jQuery來做到這一點。
$.get("http://ipinfo.io", function(response) {
console.log(response.city);
}, "jsonp");
您可以使用javascript獲取經度和緯度。
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
隨着經緯度的增長,您可以使用Google API執行反向搜索並獲取城市 –
沒有任何一個護舷幫你嗎?請標記他們接受。 – Ofir