【发布时间】:2021-12-23 17:36:45
【问题描述】:
我正在测试以确定我当前的位置是否在位置半径 (300m) 内。 如果是,则返回 true。否则返回false
请在下面找到当前正在使用的代码
<body>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
//below returant location
var resturantLat = 47.55522178620101;
var resturantLon = -122.34152897937625;
var radius = 300
//show my current location in page
var myLocation = document.getElementById("demo");
//get my current location
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
myLocation.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
myLocation.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
var myLocationLat = position.coords.latitude;
var myLocationLon = position.coords.longitude;
/*
if (my location inside radius){return true} else { return false}
*/
</script>
</body>
【问题讨论】:
-
如果您当前位置与圆心之间的距离小于圆的半径,则您在里面。如果它更大,那么你就在外面。
标签: javascript html web geolocation maps