我的应用程序使用离子时出现问题。在我的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>
.........
..........
.........
EExcuse我,我已经改变了一些代码,因为我在发布问题之前写了一些文本,我在控制器中注入了位置。 position.positioning()效果很好,但在GPS关闭和功能的onError进入,应用程序给我在接下来的页面,但这种只顾如果函数的onSuccess – Michael
@迈克尔请看看我的更新回答。 – Deurco
你能写一个承诺的例子吗? – Michael