각도 프로젝트에서 다음 위치 정보 서비스를 사용하여 위치를 얻으려고합니다. 서비스 파일 :각도 Geolocation 서비스에서 아무 것도 반환하지 않습니다.
angular.module('myApp')
.service('geolocation', function ($q, $http) {
var getLocation = function() {
var defer = $q.defer();
// If supported and have permission for location...
if (navigator.geolocation) {
//
navigator.geolocation.getCurrentPosition(function(position){
var result = {latitude : position.coords.latitude , longitude : position.coords.longitude}
// Adding randomization since we are all in the same location...
result.latitude += (Math.random() >0.5? -Math.random()/100 : Math.random()/100 );
result.longitude += (Math.random() >0.5? -Math.random()/100 : Math.random()/100 );
getNearbyCity(result.latitude, result.longitude).then(function(data){
result.address = data.data.results[1].formatted_address;
defer.resolve(result);
});
}, function(error){
defer.reject({message: error.message, code:error.code});
});
}
else {
defer.reject({error: 'Geolocation not supported'});
}
return defer.promise;
}
var getNearbyCity = function (latitude, longitude){
var defer = $q.defer();
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude +',' + longitude +'&sensor=true';
$http({method: 'GET', url: url}).
success(function(data, status, headers, config) {
defer.resolve({data : data});
}).
error(function(data, status, headers, config) {
defer.reject({error: 'City not found'});
});
return defer.promise;
}
var service = {
getLocation : getLocation,
getNearbyCity: getNearbyCity
};
return service;
});
그리고 내 컨트롤러를 호출
angular.module('myApp')
.controller('HomeCtrl', function ($scope, geolocation) {
geolocation.getLocation().then(function(result){
$scope.location = result;
console.log($scope.location);
});
});
불행히도을 console.log()는 아무 것도 반환하지 않습니다. 여기서 내가 뭘 잘못하고 있니?
, 그것은 작동하지 않습니다. 플 런커가 작동하지 않습니다. – Ericel
작동합니다! 버튼을 클릭해야합니다. 이제 ng-init으로 변경했습니다. – Sajeetharan
이것은 내 Windows 컴퓨터에서 잘 작동합니다. 하지만 리눅스에서는 그렇지 않다. 도대체 무슨 일이 일어나는지 확인하겠습니다. 당신의 노력에 감사드립니다. 감사합니다. – Ericel