提问人:phqn2099 提问时间:7/22/2023 更新时间:7/22/2023 访问量:28
根据两个位置坐标重定向到特定页面
Redirecting to a specific page based on two location coordinates
问:
我正在尝试编写一个PHP页面,其中:
该页面将验证两个不同的坐标。第一个坐标将被硬编码。第二个坐标将基于用户的当前位置。
该页面将验证用户的当前位置是否为硬编码位置的 X 半径(以码为单位)。
如果用户的当前位置是 X >,那么距离硬编码位置 300 码,请重定向到此页面“允许.php。如果没有,请重定向到“拒绝.php”。
这是PHP脚本
<?php
/**
* Calculate the Haversine distance between two points on the Earth's surface
*
* @param float $lat1 The latitude of the first point
* @param float $lon1 The longitude of the first point
* @param float $lat2 The latitude of the second point
* @param float $lon2 The longitude of the second point
* @return float The distance between the two points, in kilometers
*/
function haversine_distance($lat1, $lon1, $lat2, $lon2)
{
$radius = 6371; // Earth's radius in kilometers
// Calculate the differences in latitude and longitude
$delta_lat = $lat2 - $lat1;
$delta_lon = $lon2 - $lon1;
// Calculate the central angles between the two points
$alpha = $delta_lat / 2;
$beta = $delta_lon / 2;
// Use the Haversine formula to calculate the distance
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta));
$c = asin(min(1, sqrt($a)));
$distance = 2 * $radius * $c;
// Round the distance to four decimal places
$distance = round($distance, 4);
return $distance;
}
// Sample usage
$lat1 = 42.291093498546616;
$lon1 = -71.06439611612645;
$lat2 = 42.2707685745094;
$lon2 = -71.0237761587225;
$distance = haversine_distance($lat1, $lon1, $lat2, $lon2);
echo "The distance between ($lat1, $lon1) and ($lat2, $lon2) is $distance km.";
</php>
获取两个坐标:
<!DOCTYPE html>
<html>
<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);
} 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>
</body>
</html>
我似乎无法将这两个脚本拼凑在一起以根据需要工作。我是编码世界的新手,需要一些学习课程和技巧。请帮忙。非常感谢。
答:
0赞
IT goldman
7/22/2023
#1
您应该将此数据(位置)发布到服务器,以便可以检索它并计算半径并相应地重定向。您可以使用表单提交。
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
var loc = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
}
var data = JSON.stringify(loc);
some_form.loc = data;
some_form.submit();
}
<form id="some_form" action="radial-redirect.php">
<input type="hidden" name="loc">
</form>
在服务器上:
// radial-redirect.php
$data = $_REQUEST['loc'];
$obj = json_decode($data, true);
$distance = haversine_distance($lat1, $lon1, $obj["latitude"], $obj["longtitude"]);
if ($distance > 40) {
$newURL = "this.php";
} else {
$newURL = "that.php";
}
header('Location: '.$newURL);
评论