如何使用HTML5地理定位本地API?

时间:2021-01-13 19:37:40

I'm using following code snippet to obtain the current geolocation:

我使用以下代码片段获得当前的地理位置:

angular.module('HomelandApp').controller('PlacesController', function($scope,$http, $localStorage, uiGmapGoogleMapApi) {

  uiGmapGoogleMapApi.then(function(maps) {

        navigator.geolocation.getCurrentPosition(function (pos) {
            $scope.map = { center: { latitude: pos.coords.latitude, longitude: pos.coords.longitude }, zoom: 17 };
        });
  });
});

The problem I'm facing is that I somehow have to invoke the controller twice in order to get the map rendered. This is happening when using navigator so it's not a pure Angular UI problem.

我面临的问题是,我必须调用控制器两次才能得到渲染的映射。这是在使用导航器时发生的,所以这不是一个纯粹的角度UI问题。

I guess they're digest cycle Angular against native JS details. How should I fix this issue?

我猜它们是针对本地JS细节的消化循环。我该如何解决这个问题?

1 个解决方案

#1


2  

navigator.geolocation is outside of angular core so any scope changes made within it need to notify angular to run a digest to update the view. There are several ways to do that.

导航器。地理定位是在角度核心之外的,所以在它的范围内的任何变化都需要通知角来运行摘要来更新视图。有几种方法可以做到这一点。

Try:

试一试:

 navigator.geolocation.getCurrentPosition(function (pos) {
            $scope.map = { .... };
            $scope.$apply();
});

#1


2  

navigator.geolocation is outside of angular core so any scope changes made within it need to notify angular to run a digest to update the view. There are several ways to do that.

导航器。地理定位是在角度核心之外的,所以在它的范围内的任何变化都需要通知角来运行摘要来更新视图。有几种方法可以做到这一点。

Try:

试一试:

 navigator.geolocation.getCurrentPosition(function (pos) {
            $scope.map = { .... };
            $scope.$apply();
});