2014-06-05 69 views
0

我在我的模板之一有一個僞指令中的GoogleMaps V3 domListener功能,以下是該指令的代碼:的PhoneGap沒有發射Angularjs指令

appDirectives.directive('map', function() { 
    return { 
    restrict: 'E', 
    scope: { 
     onCreate: '&' 
    }, 
    link: function ($scope, $element, $attr) { 

     alert("This fires just fine."); 

     function initialize() { 

     alert("This doesnt fire on Phonegap."); 

     navigator.geolocation.getCurrentPosition(function (pos) { 
      $scope.currentLocation = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
      var mapOptions = { 
      center: $scope.currentLocation, 
      zoom: 15, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      disableDefaultUI: true 
     }; 

     var map = new google.maps.Map($element[0], mapOptions); 

     var currentLocation = $scope.currentLocation; 

     $scope.onCreate({ 
      map: map, 
      currentLocation: currentLocation 
     }); 

     // Stop the side bar from dragging when mousedown/tapdown on the map 
     google.maps.event.addDomListener($element[0], 'mousedown', function (e) { 
      e.preventDefault(); 
      return false; 
     }); 

     }, function (error) { 
      alert('Erro ao obter localização.'); 
     }); 
     } 

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

    } 
    } 

當運行在瀏覽器上的應用程序,一切按預期工作。但是,在iOS模擬器上運行時,它並不會觸發函數initialize()。

我已經試過這(如描述here):

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

卜那麼它只是失敗,無論是在瀏覽器和simualtor工作。

任何想法爲什麼?

回答

1

在獲取當前位置之前,您必須確保cordova已準備就緒。這裏是PhoneGap的文檔的解釋

http://docs.phonegap.com/en/1.7.0/cordova_events_events.md.html#deviceready

編輯:

這裏是你如何在角方式使用deviceReady在api.js您儲存服務

//cordova service 
    apiServices.factory('cordovaReady', function() { 
     return function (fn) { 

      var queue = []; 

      var impl = function() { 
       queue.push(Array.prototype.slice.call(arguments)); 
      }; 

      document.addEventListener('deviceready', function() { 
       queue.forEach(function (args) { 
        fn.apply(this, args); 
       }); 
       impl = fn; 
      }, false); 

      return function() { 
       return impl.apply(this, arguments); 
      }; 
     }; 
    }); 

    //map service 
    apiServices.factory('geolocation', function ($rootScope, cordovaReady) { 
     return { 
      getCurrentPosition: cordovaReady(function (onSuccess, onError, options) { 
       navigator.geolocation.getCurrentPosition(function() { 
         var that = this, 
          args = arguments; 

         if (onSuccess) { 
          $rootScope.$apply(function() { 
           onSuccess.apply(that, args); 
          }); 
         } 
        }, function() { 
         var that = this, 
          args = arguments; 

         if (onError) { 
          $rootScope.$apply(function() { 
           onError.apply(that, args); 
          }); 
         } 
        }, 
        options); 
      }) 
     }; 
    }); 

然後在您的控制器中注入geoLocation服務

geolocation.getCurrentPosition(function (position) { 

    $scope.position = { 
     coords: { 
      latitude: position.coords.latitude, 
      longitude: position.coords.longitude 
     } 
    }; 
}); 
+0

這是最好包括修復的相關部分在stackoverflow答案,這種方式答案保留在鏈接腐爛的情況下。 – mdewitt

+0

@mdewitt它我的壞。希望現在好了 – Burhan