2015-09-07 61 views
2

我的应用程序使用离子时出现问题。在我的Service.js中,我有一个函数可以检索我的位置,并在我的controller.js中有一个函数,当我按下我的html页面中的按钮时,将用户发送到另一个页面。所以,当我按下按钮时,在应用程序发送给另一个页面之前,我希望在我的service.js(geolocation)中启动该函数,并且只有在地理位置成功时才发送给我。使用服务变量的控制器

我该怎么做?

我想我可以在service.js上保存一个变量。如果函数进入onSuccess,我保存变量= 1,如果函数输入错误,我保存变量= 0。但我不知道如何在我的控制器上使用此变量...

service.js:

.factory('position', function() { 
    return{ 
    positioning : function(){ 
    var geocoder; 
    geocoder = new google.maps.Geocoder(); 
    initialise = function() { 

     navigator.geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy : true, timeout : 5000, maximumAge : 0}); 
    function onSuccess(position){ 


     myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
    } 
    function onError(error){ 
     alert('code: ' + error.code + '\n' + 
       'message: ' + error.message + '\n'); 
     cordova.plugins.settings.openSetting("open", function(){console.log("opened settings")},function(){console.log("failed to open nfc settings")}); 
    } 

    } 
    google.maps.event.addDomListener(window, 'load', initialise()); 

Controller.js:

.controller('MainCtrl', function($scope,$state, position) { 
    $scope.mappa=function(){ 
     $state.go('tab.mappa'); 
    position.positioning(); 

    } 
    $scope.dettagli=function(){ 
    $state.go('tab.dettagli'); 
    position.positioning(); 
    } 
    $scope.strada=function(){ 
    $state.go('tab.strada'); 
    position.positioning(); 
    } 
    $scope.segnalazioni=function(){ 
    $state.go('tab.segnalazioni'); 
    position.positioning(); 
    } 
    $scope.SOS=function(){ 
    $state.go('tab.sos'); 
    position.positioning(); 
    } 

html页面:

<ion-view view-title="Home" ng-controller="MainCtrl"> 
    <ion-content class="padding"> 
    <div > 
    <!-- LA MIA POSIZIONE --> 
     <div class="max-width width-forty"> 
      <button class="button button-full button-royal" ng-click="mappa()"> 
       <table> 
       <tr><td><i class="fa fa-map fa-hx fa-inverse"></i></td></tr> 
       <tr><td class="button-text-map-sos">La mia posizione</td></tr> 
       </table> 
      </button> 
      </div> 
......... 
.......... 
......... 

回答

1

当您使用ui-router时,您可以使用返回承诺并检查地图的服务来检查位置,如果没有位置则显示错误,否则显示错误。

.config(function($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise('/'); 
    $stateProvider.state('home', { 
     url: '/', 
     templateUrl: 'page1.html', 
     controller: 'page1Ctrl' 
     }) 
     .state('map-view', { 
     url: '/map-view/:forceBad', 
     templateUrl: 'page2.html', 
     controller: 'page2Ctrl', 
     resolve: { 
      location: function(GeoLocation, $q, $stateParams) { 
      return GeoLocation.getLocation($stateParams.forceBad) 
       .then(function(_response) { 
       return _response; 
       }, function(_error) { 
       alert('error getting location') 
       return $q.reject("no location found"); 
       }); 
      } 
     } 
     }); 
    }) 

在我们传递一个标志,以强制坏的或好的位置,这样你可以看到在应用程序的两条路径路由

.controller('page1Ctrl', function($scope, $state) { 
    // click a button either force bad result or get good result 
    $scope.clicked = function(_forceBad) { 
     alert('clicked with forceBad ' + _forceBad); 
     $state.go('map-view', { 
     forceBad: _forceBad 
     }); 
    } 
    }) 

在服务样本控制器,我们返回时forceBad设置错误到true

.service('GeoLocation', function($state, $q) { 
    return { 
     /** 
     * when passed true, the service will not return a location 
     */ 
     getLocation: function(_forceBad) { 
     var deferred = $q.defer(); 
     setTimeout(function() { 

      if (_forceBad === "true") { 
      return deferred.reject({ 
       type: 'error', 
      }); 
      } 
      return deferred.resolve({ 
      type: 'location', 
      lat: 100, 
      lng: -100 
      }) 
     }, 200); 
     return deferred.promise; 
     } 
    } 
    }) 

See complete codePen Here

0

的主要问题似乎是在F表明你没有注入你的position工厂到你的MainCtrl控制器。

请阅读角一些关于依赖注入:

编辑点评后:

好吧,我想我明白你的问题好一点。 做你想做的事情的好方法是使用promises。 您的positioning函数应该返回一个承诺。这样,你的控制器就会知道该位置是否被找到。

ngCordova$cordovaGeolocation工厂应该可以帮助您做到这一点(请参阅getCurrentPosition的示例)。

​​可以帮助您“在您的应用程序中创建地图”部分。

+0

EExcuse我,我已经改变了一些代码,因为我在发布问题之前写了一些文本,我在控制器中注入了位置。 position.positioning()效果很好,但在GPS关闭和功能的onError进入,应用程序给我在接下来的页面,但这种只顾如果函数的onSuccess – Michael

+0

@迈克尔请看看我的更新回答。 – Deurco

+0

你能写一个承诺的例子吗? – Michael

相关问题