我尝试使用服务在我的控制器中创建一个范围。
服务代码如下:
angular.module('GoogleMapsService', []).service('GoogleMaps', [
'$cordovaGeolocation', '$ionicLoading', '$ionicPlatform', '$q', function($cordovaGeolocation, $ionicLoading, $ionicPlatform, $q) {
var GoogleMaps, addMarkerOnMap, convertCoordinatesToFormattedAddress, getGPSCoordinatesFromDevice, initGoogleMap, mapDetails, useAddress, useGPSCoordinates;
GoogleMaps = this;
GoogleMaps.deferred = null;
mapDetails = {
lat: '',
long: '',
address: ''
};
getGPSCoordinatesFromDevice = function() {
var coordinates, cordovaGPSModuleOptions;
$ionicLoading.show({
template: '<ion-spinner icon="bubbles"></ion-spinner><br/>Acquiring location!'
});
coordinates = {};
cordovaGPSModuleOptions = {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 0
};
return $cordovaGeolocation.getCurrentPosition(cordovaGPSModuleOptions).then((function(position) {
var lat, long;
lat = position.coords.latitude;
long = position.coords.longitude;
mapDetails.lat = lat;
mapDetails.long = long;
return coordinates = {
lat: lat,
lng: long
};
}));
};
convertCoordinatesToFormattedAddress = function(map, coordinates) {
var geocoder;
geocoder = new google.maps.Geocoder();
return geocoder.geocode({
location: coordinates
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
return mapDetails.address = results[0].formatted_address;
} else {
return console.log("unable to use reverse geocode to get formatted address for coordinates provided");
}
});
};
addMarkerOnMap = function(map, coordinates) {
var marker;
marker = new google.maps.Marker({
map: map,
position: coordinates,
title: 'Location',
draggable: true,
infoWindow: {
content: "<div><h3>Move me!</h3></div>"
}
});
return marker;
};
initGoogleMap = function(coordinates) {
var map;
map = new google.maps.Map(document.getElementById('map'), {
center: coordinates,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
addMarkerOnMap(map, coordinates);
convertCoordinatesToFormattedAddress(map, coordinates);
return map;
};
useGPSCoordinates = function() {
GoogleMaps.deferred = $q.defer();
console.log(getGPSCoordinatesFromDevice().then(function(value) {
var gpsCoordinates;
gpsCoordinates = value;
initGoogleMap(gpsCoordinates);
return $ionicLoading.hide();
}));
return GoogleMaps.deferred.promise;
};
useAddress = function() {
return console.log("using address");
};
return GoogleMaps = {
'useGPS': useGPSCoordinates,
'useAddress': useAddress,
'mapDetails': mapDetails
};
}
]);
它返回并且对象名为GoogleMaps
。
在我的控制器中,我将此服务称为以下内容:
$scope.evtClickUseGPSPosition = function() {
var gpsConfirm;
gpsConfirm = {
modalClass: "modal-gps-confirm",
modalMessage: 'We will try to detect your current position automatically using your device GPS. Be sure the GPS is enabled and, if asked, give the app permission to use GPS',
confirmText: 'Locate'
};
return myModals.confirm(gpsConfirm, $scope).then(function(res) {
if (res === true) {
return GoogleMaps.useGPS();
} else {
return console.log("cancelled...");
}
});
};
基本上,当有人点击一个按钮时,我会显示一个要求确认的模态(模态是我拥有的另一个服务),并且在模态承诺解析时,我会调用创建地图的服务。
一切正常,但我需要能够创建一个名为&#34;地址&#34;使用来自GoogleMaps-mapDetails.address的结果值,但是在我第一次单击时返回空,而不是第二次清空。
我认为这与承诺的解决方式有关,但我无法弄清楚如何解决这个问题。
任何帮助?
由于