2014-08-31 58 views
2

我使用Google Maps API獲取兩點之間的距離。此方法使用回調函數。問題在於,這段代碼位於Angular服務中,我想從控制器調用該服務,以便控制器可以在範圍中輸出結果。但我不知道該服務如何將所產生的對象數組返回給控制器。我怎樣才能做到這一點?角度服務中的回調 - 如何將數據返回給控制器

myAppServices.service('NearestHospitalService', function(){ 
function calculateDistances() { 
     var service = new google.maps.DistanceMatrixService(); 
     var hospitals = []; 
     service.getDistanceMatrix(
     { 
      origins: [origin1], 
      destinations: [destinationA, destinationB], 
      travelMode: google.maps.TravelMode.DRIVING, 
      unitSystem: google.maps.UnitSystem.METRIC, 
      avoidHighways: false, 
      avoidTolls: false 
     }, callback); 

    } 

function callback(response, status) { 
     if (status != google.maps.DistanceMatrixStatus.OK) { 
      alert('Error was: ' + status); 
     } 
     else { 
      var origins = response.originAddresses; 
      var destinations = response.destinationAddresses; 
      var results = response.rows[0].elements; 
      for (var j = 0; j < results.length; j++) { 
       hospitals.push(new hospital(destinations[j], results[j].distance, results[j].duration)); 
      } 
      hospitals.sort(compareDistance); 
     } 
    } 
}); 

回答

4

我會添加一些僞代碼來讓你走向正確的方向。基本上使用回調函數,您需要回調回到您想要使用您的價值的位置。

AngularJS有一個用於此的未來/承諾模塊,也稱爲$ q。我不會使用它,但你應該看看它。

myAppServices.service('NearestHospitalService', function(){ 
    var api = {}; 
    api.calculateDistances = function(callBack) { 
    var service = new google.maps.DistanceMatrixService(); 
    service.getDistanceMatrix(
    { 
     origins: [origin1], 
     destinations: [destinationA, destinationB], 
     travelMode: google.maps.TravelMode.DRIVING, 
     unitSystem: google.maps.UnitSystem.METRIC, 
     avoidHighways: false, 
     avoidTolls: false 
    }, extractResult); 

    function extractResult(response, status) { 
     ... do computations on you response ... 
     callback(transformedResponse); 
    } 
    }; 

    return api; 
}); 

你再這樣稱呼它:

$scope.distances = []; 
NearestHospitalService.calculateDistances(function(ds) { $scope.distances = ds; }); 

另一種方式可能是注入$ rootScope,如果你在你的代碼,需要知道的東西,已經多個部件使用廣播更新以便他們可以提取他們需要的東西。

在這種情況下,你能緩存在服務

myAppServices.service('NearestHospitalService', ['$rootScope', function($rootScope){ 
    var api = {}; 
    api.distances = []; 
    api.calculateDistances = function() { 
    var service = new google.maps.DistanceMatrixService(); 
    service.getDistanceMatrix(
    { 
     origins: [origin1], 
     destinations: [destinationA, destinationB], 
     travelMode: google.maps.TravelMode.DRIVING, 
     unitSystem: google.maps.UnitSystem.METRIC, 
     avoidHighways: false, 
     avoidTolls: false 
    }, extractResult); 

    function extractResult(response, status) { 
     ... once again calculate ... 
     api.distances = transformedResult; 
     $rootScope.$broadcast('updatedDistances'); // you could also send distances as an argument directly if you wanted to. 
    } 
    }; 

    return api; 
}]); 

響應然後,你需要收聽廣播並獲取緩存。

$rootScope.$on('updatedDistances', function(event) { $scope.distances = NearestHospitalService.distances }); 
+0

謝謝你的幫助Magnus! 我有一個快速的問題 - 什麼是var api = {}在做什麼,你爲什麼要返回它? – bjornasm 2014-08-31 13:41:44

+0

它使它成爲具有功能的對象。在你的例子中,根本沒有服務的返回值。 – Magnus 2014-08-31 13:44:56

+0

謝謝。關於控制器中的功能(ds),什麼是ds? – bjornasm 2014-08-31 13:55:24