【问题标题】:Geolocation, latitude and longitude not defined after first call首次通话后未定义地理位置、纬度和经度
【发布时间】:2018-05-29 20:57:50
【问题描述】:

我有一个按钮,它调用一个名为 HomeTest() 的函数。在该函数中,我调用 geoFindMe(),一个简单的地理定位函数。这个 geoFindMe() 给了我一个 var latitude 和 var longitude。在 HomeTest() 中,我使用这些变量来查看我是否在某个多边形中。 如果我在正确的多边形中,按钮应该更改为另一个 .html 文件

我的问题是,我必须按两次按钮才能使网站加载到新的 .html 文件上,因为它似乎没有使用第一次尝试,即使我在使用变量之前调用了 geoFindMe()。我对 js 有点陌生,所以我不太确定为什么当我在正确的区域时单击后不会重新定位到新的 .html 文件。 有人知道吗?

function geoFindMe() {
  if (!navigator.geolocation){
    output.innerHTML = "<p>Your browser doesn't support geolocation.</p>";
    return;
  }

  function success(position) {
    latitude  = position.coords.latitude;
    longitude = position.coords.longitude;

  };

  function error() {
    output.innerHTML = "The site was not able to locate you";
    alert("Please use another browser.");
  };


  navigator.geolocation.getCurrentPosition(success, error);
}


function HomeTest(){
    geoFindMe();
    var polygon = [ [ longitude1, latitude1], [ longitude2, latitude2], [ longitude3, latitude3], [ longitude4, latitude4] ];
    insideTest([ longitude, latitude ], polygon); // true -> point coordinates that are searched
   //alert("is inside is " + isInsideTest + " " + latitude + " " + longitude);
    //Test_LockButton();
    if(isInsideTest){
        location.href = './html/testhome.html';
    }
}

这是检查纬度和经度是否在多边形的4个点内的功能(见上文)

function insideTest(point, vs) {

    var x = point[0], y = point[1];

    for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
        var xi = vs[i][0], yi = vs[i][1];
        var xj = vs[j][0], yj = vs[j][1];

        var intersect = ((yi > y) != (yj > y))
            && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
        if (intersect) isInsideTest = !isInsideTest;
    }

    return isInsideTest;
}

【问题讨论】:

    标签: javascript button geolocation location latitude-longitude


    【解决方案1】:

    这是一个非常简单的问题,导致您如此悲痛,getCurrentPosition 函数是异步的,这意味着您在调用 insideTest 时还没有结果。当您第二次单击时,变量已被存储,一切都按预期工作。如果你在调用 insideTest 之前延迟几百毫秒,一切都会好起来的,但这不是很好的做法,最好定义一个回调函数在位置可用时调用,这里有一个例子:

    function insideTest(point, vs) {
    
        var isInsideTest = false;
        var x = point[0], y = point[1];
    
        for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
            var xi = vs[i][0], yi = vs[i][1];
            var xj = vs[j][0], yj = vs[j][1];
    
            var intersect = ((yi > y) != (yj > y))
                && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) isInsideTest = !isInsideTest;
        }
    
        return isInsideTest;
    }
    
    function geoFindMe(successCallback) {
        if (!navigator.geolocation){
            output.innerHTML = "<p>Your browser doesn't support geolocation.</p>";
            return;
        }
    
        function success(position) {
            latitude  = position.coords.latitude;
            longitude = position.coords.longitude;
            successCallback();
        };
    
        function error() {
            output.innerHTML = "The site was not able to locate you";
            alert("Please use another browser.");
        };
    
        navigator.geolocation.getCurrentPosition(success, error);
    }
    
    function homeTestCallback() {
        var polygon = [ [ longitude1, latitude1], [ longitude2, latitude2], [ longitude3, latitude3], [ longitude4, latitude4] ];
        var isInsidePolygon = insideTest([ longitude, latitude ], polygon);
        if(isInsidePolygon) {
            location.href = './html/testhome.html';
        }
    }
    
    function HomeTest() {
        // Pass in a function to call when the position is ready
        geoFindMe(homeTestCallback);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2016-07-26
      • 1970-01-01
      • 2015-12-06
      • 2013-10-01
      • 1970-01-01
      相关资源
      最近更新 更多