私はジオロケーションと逆ジオコーディングアプリケーションを実行しています。機能は、私のコントローラの機能を私のサービスに呼び出すためにボタンをクリックすることに拘束されています。サービスはいくらか動作しますが、値を取得していますが、コントローラに値を返すことができません。AngularJS - コントローラまたはビューに戻り値を取得しない
私はこれまでの約束とリターンでいくつかの問題を抱えていましたが、そのうちのいくつかは私が解決しましたが、明らかにすべてではありません。ヘルプは高く評価されます。
マイサービス 'geoService':
(function() {
'use strict';
angular.module('JourneyApp').factory('geoService',
[
'$q',
function($q) {
var geoServiceFactory = {};
function getLocation(location) {
var deferred = $q.defer();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, error);
} else {
console.log("No support for Geolocation");
deferred.reject(false);
}
return deferred.promise;
}
function error(error) {
console.log(error);
}
function showPosition(position) {
var deferred = $q.defer();
var geocoder = new google.maps.Geocoder();
var coords = position.coords;
var latlng = { lat: parseFloat(coords.latitude), lng: parseFloat(coords.longitude) };
geocoder.geocode({ 'location': latlng },
function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
console.log(results);
if (results[0]) {
var formattedAddress = results[0].formatted_address;
console.log(formattedAddress);
deferred.resolve(formattedAddress);
} else {
console.log("No match, sorry");
deferred.reject("Error");
}
} else {
console.log("Error, sorry");
deferred.reject("Error");
}
});
return deferred.promise;
}
geoServiceFactory.getLocation = getLocation;
geoServiceFactory.showPosition = showPosition;
return geoServiceFactory;
}
]);
})();
そして、これは私のコントローラである:
(function() {
'use strict';
angular.module('JourneyApp').controller('tripsController',
[
'tripsService', 'vehicleService', 'geoService', function(tripsService, vehicleService, geoService) {
//'tripsService', function (tripsService) {
var vm = this;
// Get start geolocation
vm.getStartLocation = function() {
geoService.getLocation().then(function (location) {
vm.trip.startAdress = location;
});
}
// Get stop geolocation
vm.getStopLocation = function() {
geoService.getLocation().then(function(location) {
vm.trip.stopAdress = location;
});
}
}
]);
}());
そして、私のビューの部品:
<div class="col-md-2">
<input ng-model="vm.trip.startAdress"/>
</div>
<div class="col-md-2">
<button ng-click="vm.getStartLocation()">Get location</button>
</div>
私はここで間違って何をやっていると、どうすれば修正できますか?
:以下は、トリックを(このコードで
getLocation
であれば交換)を行う必要があります。あなたはそれをしなければなりません。これは動作させるための最初のステップですが、showPositionも約束を返すので、それを解決する方法を見つけなければなりません。 – Riiverside