【问题标题】:getCurrentPosition does not work with angularjsgetCurrentPosition 不适用于 angularjs
【发布时间】:2016-01-12 03:34:50
【问题描述】:

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

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

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

【问题讨论】:

标签: javascript angularjs geolocation ionic-framework


【解决方案1】:

你好它为我工作==========

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}}

您在离子问题中的上述代码在没有声明的情况下不访问直接值。

【讨论】:

  • 感谢分享您的代码。是的,问题是声明
猜你喜欢
  • 1970-01-01
  • 2018-04-27
  • 2017-07-26
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
相关资源
最近更新 更多