2015-10-14 59 views
0

我得到了一个视图,我呈现一个谷歌地图,然后应该定期检查设备的位置。离子角工厂返回undefined

控制器

.controller('SearchCtrl', function($scope, $ionicLoading, TrackLocation) { 
    //document.addEventListener("deviceready", onDeviceReady, false); 
    //function onDeviceReady() { 

     //Loads Google Map 
     var mapOptions = { 
      center: new google.maps.LatLng(0,0), 
      zoom: 16, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      mapTypeControl: false, 
      streetViewControl: false 
     }; 
     var map = new google.maps.Map(document.getElementById("map"), mapOptions); 

     var myPost = TrackLocation.track(); 
     console.log(myPost); 
    //} 
}) 

.factory('TrackLocation', function($http, $cordovaGeolocation) { 
    return { 
     track: function() { 

      console.log("Started Tracking"); 
      var lat, long; 

      var watchOptions = { 
       timeout : 3000, 
       enableHighAccuracy: true 
      }; 

      var watch = $cordovaGeolocation.watchPosition(watchOptions); 
      watch.then(
      null, 
      function(err) { 
       console.log(err); 
       return "lole"; 
      }, 
      function(position) { 
       lat = position.coords.latitude 
       long = position.coords.longitude 
       console.log(lat); 
      }); 
      return {'lat': lat, 'long':long }; 
     }, 
     stop: function() { 

     } 
    }; 

}) 

控制台登录

Started Tracking 
Object {lat: undefined, long: undefined} 
103 14.408132700000001 

的myPost是返回一个未定义的值,因为它被称为前的实际价值是通过对土地增值税。

请帮忙。

+0

你在'watch.then'完成之前返回结果并且你设置了你的变量 – Grundy

+0

我试着把'return {'lat':lat''long':long};'放在'function(position)我是一个普通的未定义..不是一个对象,但明确未定义。 – Roi

+0

当然,因为在这种情况下,您确实从'watch'回调函数返回,而不是'track'函数 – Grundy

回答

0

你应该只是在你的跟踪方法中返回或者返回promise,而不是在成功回调中用lat和long值解决这个承诺。