【问题标题】:Cordova Geolocation load after all the elementCordova Geolocation 在所有元素之后加载
【发布时间】:2018-08-11 12:36:12
【问题描述】:

我面临科尔多瓦地理位置的问题。返回值需要时间。我使用了超时,但它没有按预期工作。我不能让用户等待太多时间。

这里是代码。

navigator.geolocation.getCurrentPosition(function(position) {
   $scope.userlongitude = position.coords.longitude;
   $scope.userlatitude = position.coords.latitude;

   console.log($scope.userlatitude);
   console.log($scope.userlongitude);                             
});

这是我使用地理定位的 Ajax 代码。

$timeout(function() {
   $http({
      url: 'suggestions.php',
      method: "GET",
      params: {
         latitude: $scope.userlatitude,
         longitude: $scope.userlongitude
      }
   })
   .success(function(data) {
      $scope.news_suggestions = data.content;
   });
}, 3000);

上面的代码我在地理位置返回之前使用了 http 请求触发。

如何解决问题。

编辑:

我尝试了设备就绪事件,但问题未解决。

  document.addEventListener("deviceready", onDeviceReady, false);
     function onDeviceReady() {
     -------- Codes are here. ----------
    }
});

【问题讨论】:

  • 您可以在设备就绪事件中调用它
  • 我尝试了设备就绪事件,但问题仍然存在。在地理位置发送数据之前触发 Ajax。

标签: javascript android cordova


【解决方案1】:

https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition 请使用默认回调函数

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

    var options = {};

    function success(position) {
      $scope.userlongitude = position.coords.longitude;
      $scope.userlatitude = position.coords.latitude;

      $http({
          url: 'suggestions.php',
          method: "GET",
          params: {
             latitude: $scope.userlatitude,
             longitude: $scope.userlongitude
          }
       })
       .success(function(data) {
          $scope.news_suggestions = data.content;
       });
    }

    function error(err) {
      console.warn(`ERROR(${err.code}): ${err.message}`);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多