我查了一下,没有找到真正回答我所要做的事情的内容,因此我在这里发布!
我的总体目标是让页面读取用户位置,然后根据他们的位置运行代码。具体来说,我有一个Facebook签到脚本,如果用户在特定位置,则可以允许用户签到。
问题是所涉及的位置比较大,因此手动输入位置的经纬度不起作用。我现在遇到的问题是是否可能告诉JS获取位置的硬编码经度和纬度,但是给出坐标周围的半径(假设为200米),因此当用户进入坐标的200m半径时,代码将被激活。
有人有什么想法吗?
这是我目前的代码。
jQuery(window).ready(function(){
initiate_geolocation();
});
function initiate_geolocation() {
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
}
function handle_errors(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED: alert("user did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE: alert("could not detect current position");
break;
case error.TIMEOUT: alert("retrieving position timed out");
break;
default: alert("unknown error");
break;
}
}
function handle_geolocation_query(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;
//these are for testing purposes
alert('Your latitude is '+lat+' and longitude is '+long);
if (lat == 0 && long == 0) {alert('It works!');};
}
if (lat = 0 && long = 0) {alert('It works!');};
应该改为if (lat == 0 && long == 0) {alert('It works!');};
- Mark Ormston