2017-04-30 13 views
0

私はジオロケーションと逆ジオコーディングアプリケーションを実行しています。機能は、私のコントローラの機能を私のサービスに呼び出すためにボタンをクリックすることに拘束されています。サービスはいくらか動作しますが、値を取得していますが、コントローラに値を返すことができません。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> 

私はここで間違って何をやっていると、どうすれば修正できますか?

+0

:以下は、トリックを(このコードでgetLocationであれば交換)を行う必要があります。あなたはそれをしなければなりません。これは動作させるための最初のステップですが、showPositionも約束を返すので、それを解決する方法を見つけなければなりません。 – Riiverside

答えて

2

私のコメントで述べたように、getLocationで作成された約束は決して解決されないため、値は決して保存されません。あなたは繰延オブジェクトを作成し、その約束を返しますが、実際にそれを解決することはありませんしているgetLocation` `で

[...] 
if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function (position) { 
     showPosition(position).then(function(formattedAddress) { 
      deferred.resolve(formattedAddress); 
     } 
    }, error); 
} else { 
[...] 
+0

右。しかし今、私がifで置き換えたとき、私は 'TypeError:null'の 'startAdress'プロパティを設定できません。 – Koalemos

+0

あなたは 'vm.trip'を初期化していません。単に 'vm.trip = {};'を 'var vm = this;'としてください。 – Riiverside

+0

魅力のように働いてくれてありがとう – Koalemos

関連する問題