AngularJS地理位置数据更新
AngularJS Geolocation data update
我正在使用AngularJS做一个移动应用程序。我想要实现的是,一旦我打开GPS,就更新地理位置数据。我该如何做到这一点?我面临的问题是,为了更新数据,我必须导航到其他页面。这些是密码。我正在从一个控制器到另一个控制器共享数据。
.factory('sharedProperties', function () {
var coordinates = {};
var getC = function () {
return coordinates;
};
var setC = function (value) {
coordinates = value;
return coordinates;
};
return {
getCoords: getC,
setCoords: setC
};
})
第一控制器
.controller('MessageController', ['$scope', 'sharedProperties', function ($scope, sharedProperties) {
var nObj = sharedProperties.getCoords();
console.log(nObj);
$scope.message = "This is my location: " + nObj.lat + ", " + nObj.lng + ". I'm around " + nObj.acc + " meters from point.";
}])
第二控制器
.controller("mainController", ['$scope', 'sharedProperties', function ($scope, sharedProperties) {
$scope.lat = "0";
$scope.lng = "0";
$scope.accuracy = "0";
$scope.error = "";
$scope.model = {
myMap: undefined
};
$scope.myMarkers = [];
$scope.showResult = function () {
return $scope.error == "";
}
$scope.mapOptions = {
center: new google.maps.LatLng($scope.lat, $scope.lng),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
$scope.showPosition = function (position) {
$scope.lat = position.coords.latitude;
$scope.lng = position.coords.longitude;
$scope.accuracy = position.coords.accuracy;
$scope.$apply();
sharedProperties.setCoords({
'lat': position.coords.latitude,
'lng': position.coords.longitude,
'acc': position.coords.accuracy
});
var latlng = new google.maps.LatLng($scope.lat, $scope.lng);
$scope.model.myMap.setCenter(latlng);
$scope.myMarkers.push(new google.maps.Marker({
map: $scope.model.myMap,
position: latlng,
title: 'You are here'
}));
}
$scope.showMarkerInfo = function (marker) {
$scope.myInfoWindow.open($scope.model.myMap, marker);
};
$scope.showError = function (error) {
switch (error.code) {
case error.PERMISSION_DENIED:
$scope.error = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
$scope.error = "Location information is unavailable."
break;
case error.TIMEOUT:
$scope.error = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
$scope.error = "An unknown error occurred."
break;
}
$scope.$apply();
}
$scope.getLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError,
{ enableHighAccuracy: true});
} else {
$scope.error = "Geolocation is not supported by this browser.";
}
}
$scope.getLocation();
}])
编辑:我设法使它像这样工作。
.controller('MessageController', ['$scope', 'sharedProperties', function ($scope, sharedProperties) {
$scope.getLoc = function () {
var nObj = sharedProperties.getCoords();
console.log(nObj);
var numbers = [nObj.lat, nObj.lng, nObj.acc];
return "This is my location: " + numbers[0].toFixed(6) + ", " + numbers[1].toFixed(6) + ". I'm around " + numbers[2].toFixed(0) + " meters from point.";
}])
在视图中,我这样写。
<textarea style="width: 100%; height: 183px;" placeholder="Message">{{getLoc()}}</textarea>
但是它在文本区显示{{getLoc()}}。我是否可以隐藏这个,只有当它获得数据时才显示?
您可以使用ng-bind-html
也就是
<textarea style="width: 100%; height: 183px;" placeholder="Message" ng-bind-html="getLoc()"></textarea>
相关文章:
- 平均值:无法将数据更新到数据库
- d3在数据更新时错误地附加了dom元素
- 使用角度传递的数据更新模态
- jQuery使用XML数据更新UL列表
- ng表DOM未使用数据更新进行更新
- 在线/离线数据更新移动最佳实践
- 使用 预测 API 数据更新的 HTML 模板
- JQuery 数据表列数据更新工作太慢
- 使用Plotly中的新数据更新图形的高性能方法
- 使用新数据更新 D3 饼图.json
- jQuery - 使用相同的数据更新 2 个或多个表单字段
- 如何在数据更新时更新可重用的 d3.js 图形
- 组合框/下拉框在数据更新后未更新
- 在 JavaScript 中的数据更新后,我的 DOM 对象不会更新
- 如何使用来自外部源的数据更新 ng 模型
- 主干.js:使用不同的数据更新集合
- 对数据库进行 AJAX 编辑时,我是否应该立即使用新数据更新接口
- 计划脚本以使用外部数据更新数据库
- 使用从 MongoDB 获取的新数据更新对象属性
- 如何每 n 分钟使用数据更新一次 Web 应用程序