我使用以下代码片段来获取当前地理位置:
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 };
});
});
});
我面临的问题是我必须调用 Controller 两次才能渲染 map 。使用 navigator
时会发生这种情况,因此这不是纯粹的 Angular UI 问题。
我猜它们是针对原生 JS 细节的 Angular 循环摘要。我该如何解决这个问题?
最佳答案
navigator.geolocation
位于 Angular 核心之外,因此在其中进行的任何范围更改都需要通知 Angular 运行摘要以更新 View 。有几种方法可以做到这一点。
尝试:
navigator.geolocation.getCurrentPosition(function (pos) {
$scope.map = { .... };
$scope.$apply();
});
关于javascript - 如何将 Angular 与 HTML5 地理定位原生 API 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002785/