javascript - getCurrentPosition 不适用于 angularjs

标签 javascript angularjs geolocation ionic-framework

我尝试这个(为了在页面加载时获取当前用户位置)

angular.module('ionicApp', ['ionic'])
.controller('MyCtrl', function($scope){
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position){
      $scope.$apply(function(){
        $scope.position = position;
      });
    });
  }
});

然后打印:

{{position.coords.latitude}}

它只是打印:position.coords.latitude

我正在使用 ionic 框架。为什么我的代码不起作用?

最佳答案

嗨,它对我有用==========

if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
 console.log("Geolocation is not supported by this browser.");
}
    var latLng = {};

  function showPosition(position) {
     latLng = {
     'lat': position.coords.latitude,
     'lng': position.coords.longitude
  };
   $scope.location_address = latLng;
  }

HTML 端中打印 {{location_address}}

您的上述代码存在 ionic 问题,无法在没有声明的情况下访问直接值。

关于javascript - getCurrentPosition 不适用于 angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735159/

相关文章:

javascript - 使用 angularJs 路由的 firefox sdk 插件

css - 使div元素拉伸(stretch)页面全高

javascript - 如何以用户位置为中心制作 map

Javascript 函数在没有被调用的情况下执行?

c# - 在 ASP.NET 页面中查找客户端位置

javascript - 使用 Passport.js 的登录系统始终执行 “failureRedirect” (nodejs)

javascript - 主干事件点击表格行

javascript - 如何根据数据属性访问数组

javascript - ckeditor中插入html按钮的onclick事件

javascript - AngularJS:如何从其他 JSON 中搜索并替换/添加 JSON 对象中的键?