【问题标题】:Function after checking for Geolocation doesn't start检查地理位置后的功能未启动
【发布时间】:2014-06-15 14:47:56
【问题描述】:

我正在构建一个小型 Web 应用程序,它采用表单输入(名称)+ 用户的地理位置并将其放入本地存储数组中。我是 JS 的新手,但我已经完成了负责名称的部分。我现在正试图将它扩展到在用户按下提交时将纬度 + 经度存储在本地存储数组中的点,但不知何故,执行此操作的函数不会启动/设置。

window.onload = function() {

// Read value from storage, or empty array
var names = JSON.parse(localStorage.getItem('locname') || "[]");
var lat = JSON.parse(localStorage.getItem('latitude') || "[]");
var long = JSON.parse(localStorage.getItem('longitude') || "[]");

function initCoords() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(saveData);
    console.log('This works');
  } else {
    showError("Your browser does not support Geolocation!");
  }
}

function saveData(data){
  console.log('But does it get here?');
  //Push Users Input value
  var data = document.getElementById("locationName").value;
  names.push(data);
  localStorage.setItem('locname', JSON.stringify(names));

  //Push Latitude
  var latitude = position.coords.latitude;
  lat.push(latitude);
  localStorage.setItem('latitude', JSON.stringify(latitude));

  //Push Longitude
  var longitude = position.coords.longitude;
  long.push(longitude);
  localStorage.setItem('longitude', JSON.stringify(longitude));
  }

document.querySelector('#saveData').onclick = initCoords;
} 

我有一个 ID 为 saveData 的按钮。我没有地理定位的这个脚本的早期版本运行良好,在那里我通过单击按钮启动了 saveData 功能。在这种情况下,我首先想检查用户是否有可用的地理位置,因此我创建了 initCoords 函数。

我尝试使用控制台日志来查看我的函数在哪里结束,它以某种方式没有到达 saveData 中的“但它到达这里”。

【问题讨论】:

    标签: javascript html geolocation


    【解决方案1】:

    我认为你的问题是这样的

    我有一个 ID 为 saveData 的按钮

    你还有一个同名的函数

    function saveData(data){ ....
    

    如果函数在全局范围内,它将被设置为window.saveData,然后当具有该ID的元素出现在函数之后时,浏览器将元素作为ID存储在window对象上,因此它存储window.saveData 作为元素并覆盖函数。

    解决方案是更改函数名称或元素 ID。

    编辑:

    这里还有一些问题:

    你没有在任何地方声明position,你有data作为回调的参数

    function saveData(data){
      var data = document.getElementById("locationName").value;
      ....
    

    这没有任何意义,你可能是说

    function saveData(position){
      var data = document.getElementById("locationName").value;
      ....
    

    事实上,您将位置对象中的数字存储在 localStorage 中,而不是数组:

    var latitude = position.coords.latitude;
      lat.push(latitude);
      localStorage.setItem('latitude', JSON.stringify(latitude));
    

    查看您如何将数据推送到纬度,但您保存的是纬度,数字,而不是数组,所以下次您尝试推送到数字而不是数组时会出现错误,应该是

    var latitude = position.coords.latitude;
      lat.push(latitude);
      localStorage.setItem('latitude', JSON.stringify(lat));
    

    但是现在您已经将数字存储在 localStorage 中,所以在您在脚本中走这么远之前您会收到一个错误,您还必须清除您的 localStorage,它可以在浏览器控制台中完成或类似这个

    localStorage.removeItem('locname');
    localStorage.removeItem('latitude');
    localStorage.removeItem('longitude');
    

    在你开始尝试工作代码之前,你必须清除它一次,它应该看起来像这样

    window.onload = function () {
    
        localStorage.removeItem('locname');
        localStorage.removeItem('latitude');
        localStorage.removeItem('longitude');
    
        // Read value from storage, or empty array
        var names = JSON.parse(localStorage.getItem('locname') || "[]");
        var lat   = JSON.parse(localStorage.getItem('latitude') || "[]");
        var long  = JSON.parse(localStorage.getItem('longitude') || "[]");
    
        function initCoords() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(saveData);
                console.log('This works');
            } else {
                showError("Your browser does not support Geolocation!");
            }
        }
    
        function saveData(position) {
            var data = document.getElementById("locationName").value;
            names.push(data);
            localStorage.setItem('locname', JSON.stringify(names));
    
            var latitude = position.coords.latitude;
            lat.push(latitude);
            localStorage.setItem('latitude', JSON.stringify(lat));
    
            var longitude = position.coords.longitude;
            long.push(longitude);
            localStorage.setItem('longitude', JSON.stringify(long));
    
        }
    
        document.getElementById('saveData').onclick = initCoords;
    }
    

    【讨论】:

    • 它以前使用 id saveData 和名为 saveData 的函数。但只是为了确保我将函数更改为 postData 并且问题仍然存在。
    • @user3708455 - 在我看来你有未定义的变量,position 定义在哪里?
    • 我不确定是否需要定义它,因为我在按照我在网上找到的一个简单示例进行操作,但他们也没有定义它。没有其余代码的地理位置代码(有点不同)也可以正常工作。我似乎无法找出为什么它不会启动 saveData。
    • @TDMW - 存在多个问题,我对其进行了测试并修复了我能找到的任何内容,请参阅上面的答案。
    • 你的回答很有道理,我现在肯定看到我做错了一些事情。但是完整的代码仍然无法以某种方式工作。我尝试在 saveData 函数中进行控制台日志记录,但没有任何结果。它似乎仍然没有到达代码的那部分。
    猜你喜欢
    • 2019-01-16
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2011-05-29
    相关资源
    最近更新 更多