2016-07-29 113 views
1

我目前正處於通過Ionic構建應用程序天氣的初始階段。現在我想實施科爾多瓦地理定位,我有錯誤Unknown provider: $cordovaGeolocationProvider <- $cordovaGeolocation我嘗試修復,但不成功,但是這會不斷給出一個錯誤時,打開它。爲了測試目的,我使用離子服務並在localhost中檢查它。

我的代碼

(function() { 
    angular.module('app.weather', ['ionic']) 

     .factory('Weather', function($q, $http) { 
      var deferred = $q.defer(); 

      function getCurrentWeather(lat, lng) { 
       var url = 'https://api.forecast.io/forecast//' + lat + ',' + lng + '?callback=JSON_CALLBACK'; 
       $http.jsonp(url) 
        .success(deferred.resolve) 
        .error(deferred.reject); 

       return deferred.promise; 
      } 

      return { 
       getCurrentWeather: getCurrentWeather 
      }; 
     }) 
     .controller('WeatherCtrl', function($scope, $cordovaGeolocation, Weather) { 
      $scope.loading = true; 

      $scope.toCelsius = function(temperature) { 
       return ((temperature - 32)/1.8).toFixed(1); 
      }; 

      $cordovaGeolocation 
       .getCurrentPosition({ 
        timeout: 10000, 
        enableHighAccuracy: false 
       }) 
       .then(function(position) { 
        var lat = position.coords.latitude; 
        var long = position.coords.longitude; 

        Weather.getCurrentWeather(lat, long).then(function(data) { 
         $scope.weatherInfo = data; 
         $scope.loading = false; 
        }, function(error) { 
         //TODO Display error message 
        }); 
       }, function(err) { 
        //TODO Display error message 
       }); 
     }); 
})(); 

代碼的html

<ion-view title="الرئيسية" id="page2" style="background-color:#FFFFFF;" class=" "> 
    <ion-content padding="true" class="has-header"> 
    <ion-pane> 
<div ng-controller="WeatherCtrl as weatherCtrl"> 
<div ng-show="loading"> 
       Carregando informações... 
      </div> 
      <div ng-hide="loading"> 
       <p> 
        Temperatura: {{toCelsius(weatherInfo.currently.temperature)}}º 
       </p> 
       <p> 
        Sensação térmica: {{toCelsius(weatherInfo.currently.apparentTemperature)}}º 
       </p> 
      </div> 
         </div> 

    </ion-content> 
</ion-view> 
+0

試試這個http://stackoverflow.com/a/36158618/ 5059916 –

+0

你有沒有包含'ng-cordova'文件? –

回答

1

請確保您有ngCordovaIonic Native安裝,否則你無法使用$cordovaGeolocation因爲它是在這些庫中的任何一個定義的包裝。您可以在Ionic Native中找到關於如何安裝的說明。最近還有一篇關於Ionic Nativehere的文章可能會引起您的興趣。

另外請記住,大多數Cordova插件在瀏覽器中不起作用,因此您可能需要在實際設備上進行測試。

+0

謝謝我,我修好了 – razak

0

$ cordovaGeolocation功能,與其他本地功能以及只能在模擬器或設備上使用。如果您從瀏覽器提供應用程序,則無法與插件進行交互,因此未定義。

+0

謝謝我修好了 – razak

5

加 'ngCordova' 你的模塊中像

(function() { 
    angular.module('app.weather', ['ionic','ngCordova']) 

再加入ngcordova.min.js文件鏈接到您的index.html

相關問題