Javascript Phonegap/AngularJS:从内部函数获取lat/long
我试图在一个函数中设置几个全局变量,这样我就可以在角度控制中使用结果 我已经尝试了我能想到的一切,但似乎无法将变量带到函数之外。我知道这可能很简单,但就是想不出来 提前感谢你的帮助Javascript Phonegap/AngularJS:从内部函数获取lat/long,javascript,angularjs,cordova,Javascript,Angularjs,Cordova,我试图在一个函数中设置几个全局变量,这样我就可以在角度控制中使用结果 我已经尝试了我能想到的一切,但似乎无法将变量带到函数之外。我知道这可能很简单,但就是想不出来 提前感谢你的帮助 onSuccess = function(position) { lat = position.coords.latitude; lon = position.coords.latitude; // alert('Latitude: ' + posi
onSuccess = function(position) {
lat = position.coords.latitude;
lon = position.coords.latitude;
// alert('Latitude: ' + position.coords.latitude + '\n' +
// 'Longitude: ' + position.coords.longitude + '\n' +
// 'Altitude: ' + position.coords.altitude + '\n' +
// 'Accuracy: ' + position.coords.accuracy + '\n' +
// 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
// 'Heading: ' + position.coords.heading + '\n' +
// 'Speed: ' + position.coords.speed + '\n' +
// 'Timestamp: ' + new Date(position.timestamp) + '\n');
};
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
// ??????
console.log(getCurrentPosition.lat);
编辑:这是控制器
我需要用lat和lon代替当前的硬编码版本:
var app = angular.module('presto-map', ["ngResource", "ngSanitize", "google-maps"]);
function mapCtrl ($scope) {
// Initialise the map
$scope.map = {
center: {
latitude: 53.58684546368308,
longitude: -1.5543620512747744
},
zoom: 8
};
}
如果您试图在console.log中丰富
lat
变量,那么问题是在onSuccess
处理程序之前调用了console.log
更新:
var app = angular.module('presto-map', ["ngResource", "ngSanitize", "google-maps"]);
function mapCtrl ($scope, $q, $window) {
function getPosition() {
var deferred = $q.defer();
$window.navigator.geolocation.getCurrentPosition(function(position) {
$scope.$apply(function() {
deferred.resolve({
latitude : latitude,
longitude : longitude,
accuracy : accuracy
});
})
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
}
// Initialise the map
getPosition().then(function(result) {
$scope.map = {
center: {
latitude: result.latitude,
longitude: result.longitude
},
zoom: 8
};
});
}
将
$window
传递给您的控制器,并在您的范围内设置lat lng
$scope.getCurrentLocation = function () {
$window.navigator.geolocation.getCurrentPosition(function(position) {
$scope.$apply(function() {
$scope.latitude = position.coords.latitude;
$scope.longitude = position.coords.longitude;
$scope.accuracy = position.coords.accuracy;
});
}, function(error) {
alert(error);
});
}
否则,如果您想在应用程序中包含一些全局变量,请使用服务。在服务中拥有所有全局数据,然后通过将服务传递给控制器来访问控制器中的数据。您试图公开哪些变量,以及这些变量的确切可见位置?可能您正在寻找$scope.lat…谢谢Andrey-我现在明白了。我现在编辑了这个问题,使它更清楚一些。我绝对不是JS方面的专家(很明显),所以我需要先设置一个运行onSuccess的函数吗?有什么建议吗?谢谢。@T2theC基本上是唯一的方法-在onSuccess中调用
console.log
。@T2theC添加了示例。如何在controller+中设置并保存到数据库中?对于控制器,我认为它类似于geolocation()。然后(function(position){$scope.position=position;},function(){$scope.message=“NA”;})代码>?我认为$scope.position
零件有问题或是什么。还有,如果我有一个$scope.post={lat:?;lon:?;}
如何更正这两个代码,如何保存它?