Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .factory('geolocation', function ($q, $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);
- });
- }
- }, {
- enableHighAccuracy: true,
- timeout: 20000,
- maximumAge: 18000000
- });
- }),
- getCurrentCity: function () {
- var deferred = $q.defer();
- this.getCurrentPosition(function (position) {
- var geocoder = new google.maps.Geocoder();
- geocoder.geocode({
- 'latLng': new google.maps.LatLng(parseFloat(position.coords.latitude), parseFloat(position.coords.longitude))
- },
- function (results, status) {
- if (status === google.maps.GeocoderStatus.OK) {
- if (results[0]) {
- angular.forEach(results[0].address_components, function (address_component, i) {
- if (address_component.types[0] === 'locality') {
- $rootScope.$apply(function(){
- deferred.resolve(address_component.long_name);
- });
- }
- });
- }
- }
- });
- }, function(error){
- deferred.resolve("fail");
- });
- return deferred.promise;
- }
- };
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement