【问题标题】:How to save geolocation data that persist within Firebase?如何保存在 Firebase 中持久存在的地理位置数据?
【发布时间】:2014-11-09 05:40:37
【问题描述】:

我想在提交输入时保存地理坐标以进行内部数据排序。

它显示坐标,即加载当前位置。但是,数据并未保存,而是不断刷新。我尝试将其另存为:

位置:{lat:'',lng:''}

如何确保保存数据而不仅仅是刷新当前位置?

导航栏控制器

 app.controller('NavCtrl', function($scope, $location, Post, Auth, $state, Geolocation) {
      $scope.post = {
        title:     '',
        lat:  '',  
        lng:  ''
      };


同一控制器下提交函数

 $scope.submitPost = function () {
    Post.create($scope.post).then(function (ref) {
    $location.path('tab/posts/' + ref.name()); 

      $scope.post = {
        title:     '',
        lat:  '',  
        lng:  ''
      };

      $scope.position = null;
      $scope.message = 'Determining location...';  

      new Geolocation().then(function (position) {
        $scope.position = position;
      }, function (reason) {
        $scope.message = 'Could not be determined';
      });


也在主控制器下

app.controller('MainCtrl', function($scope, Post) {

  $scope.posts = Post.all;
  $scope.post = {
    title:      '',
    lat:  '',       //inserted geodata to be reflected in main page
    lng:  ''
  };

【问题讨论】:

  • 我在您包含的代码中看不到任何 Firebase 的使用。鉴于您正在使用的技术数量,如果您在 fiddle/bin/plunkr 中设置问题的重现可能会更好。
  • @FrankvanPuffelen 感谢您的回复!很快就会设置并在这里发布。对此感到抱歉。
  • 如果无法重现问题,或许可以尝试简化plunkr,直到可以重现问题?鉴于您的问题的标题,必须有一种方法可以用更少的文件和技术来重现它。 stackoverflow.com/help/mcve
  • 看我的回答。如果您编写一个涉及较少技术的正确问题,您可能会得到更好的答案。诸如“为什么我的控制器被多次调用?”之类的东西应该能够在不使用 Firebase 和 Ionic 的情况下重现该行为。通过减少使用的技术数量,您可以扩大可以回答您问题的受众。

标签: javascript html angularjs geolocation firebase


【解决方案1】:

您的范围一直在重新创建。当您的 submitPost 方法运行时,该位置再次从当前的 $scope 消失。这很容易通过在其中放置一些console.log 语句来确定。

console.log('clearing position');
$scope.position = null;
$scope.message = 'Determining geolocation';

new Geolocation().then(function (position) {
  console.log('setting position');
  $scope.position = position;
}, function (reason) {
  $scope.message = 'Could not be determined';
});

你会看到它记录:

清仓

设置位置

当您最初加载页面时。然后是另一个:

清仓

当您点击“新帖子”链接时。因此,您的控制器似乎在那里重新执行,这从范围中清除了位置。

我能找到的最快解决方法是简单地在 $rootScope 上设置地理位置并从那里使用它:

//console.log('clearing position');
//if (!$scope.position) $scope.position = null;
$scope.message = 'Determining geolocation';

new Geolocation().then(function (position) {
  console.log('setting position');
  $rootScope.position = position;
}, function (reason) {
  $scope.message = 'Could not be determined';
});

////////////////////////////  NEW POST  ////////////////////////////
$scope.submitPost = function () {
  Post.create($scope.post).then(function (ref) { //postId works
    $location.path('tab/posts/' + ref.name());   // replace with postId
    console.log('submitPost: position=');console.log($scope.position);
    $scope.post = {
      title:      '',
      timestamp:  new Date().toUTCString(),
      position:  $rootScope.position
    };
  });
};

这样,当我们到达submitPost 时,该位置仍在根范围内。

随着您使用 console.log 和 Chrome Angular devtools 等工具变得更好,您可能会弄清楚控制器第二次执行的原因(从当前范围清除位置)。但这应该会让你上路。

【讨论】:

  • 我仍在试图找出原因。最初我的猜测是因为主帖子页面通过 stateprovider 附加到 PostCtrl,而导航提交按钮(也在主页上)有另一个 NavCtrl。不知何故,从 NavCtrl 中删除地理编码以插入 PostCtrl 并不能解决问题。
  • 用户的位置是一个全局属性,所以我认为将地理位置放在 $rootScope 中是有意义的。
  • 知道了,谢谢弗兰克。最后一个问题。对于 $scope.post ={} 代码的所有 3 个实例,我是否必须将位置从 position: '' 更改为 position: $rootScope.position?我认为 PostCtrl 的那个似乎没有必要……
  • 只是一个小问题,是隔离和只保存纬度/经度数据和查询,还是查询整个“位置”数据更好?如果是前者,是否会将其另存为单独的 longitude:$rootScope.longitude 和类似的 lat?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 2011-04-13
  • 2021-06-24
  • 1970-01-01
  • 2019-05-03
  • 2021-01-06
相关资源
最近更新 更多