Hi mthh,
I need a bit more of an explanation - from what I understand you want to use Geolocation without a map?
I do this in my app - basically I have a Map view and a List View. Each one has a service call that takes in Longitude and Latitude and shows the poker games within X miles of this point (this is just a SQL Server query.
This is how my List page looks: (Note, this code is still beta and I have a lot of tidying up and abstracting to do still)
"use strict";
(function () {
angular.module('pokerGameFinder.gamesListCtrl', [])
.controller('GamesListCtrl', function($rootScope, $log, $scope, $ionicPlatform, gameadrepository, findmylocation, searchData, appConfig, AnalyticsTracker, toastr, LoaderService) {
$log.info("GamesListCtrl", searchData.getLocation())
$ionicPlatform.ready(function() {
checkLocation();
});
//abstract this into a service
$scope.findAddress = function (address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address}, function(results, status) {
$log.debug(address, results, status);
if (status === google.maps.GeocoderStatus.OK) {
var location = { searchTerm: address, latitude: results[0].geometry.location.lat(), longitude: results[0].geometry.location.lng(), milesRadius:25 };
searchData.setLocation(location);
$scope.addressText = searchData.getLocation().searchTerm;
loadGamesForLocation(searchData.getLocation());
} else {
AnalyticsTracker.logError('Could not geocode this address: ' + status);
toastr.show('Address not found - please try again');
}
});
return;
};
$scope.findMyLocation = function() {
findmylocation.getCurrentLocation().then(function(location) {
searchData.setLocation(location);
$scope.gameAds = gameadrepository.getGamesForLocation(location);
$scope.addressText = searchData.getLocation().searchTerm;
}, function() {
//don't know why I have to put this here but it would not hide on iOS if not (does not fire in the error handler on the Service for some reason)
LoaderService.hide();
});
}
function checkLocation() {
//center the map on the user's location if it has not been set yet
//var location = $rootScope.searchLocation;
if (searchData.getLocation() === null) {
findmylocation.getCurrentLocation()
.then(function(location) {
searchData.setLocation(location);
$scope.addressText = searchData.getLocation().searchTerm;
loadGamesForLocation(searchData.getLocation());
}, function(error) {
//could not get current location or was denied - so do no nothing and the user can type a location - enter the default location in case they go to the List page without searching
searchData.setLocation(appConfig.startingLocation);
$scope.addressText = searchData.getLocation().searchTerm;
loadGamesForLocation(searchData.getLocation());
});
} else {
loadGamesForLocation(searchData.getLocation());
$scope.addressText = searchData.getLocation().searchTerm;
}
}
function loadGamesForLocation(location) {
//load the game for this location
gameadrepository.getGamesForLocation(location)
.$promise.then(function(gameAds) {
$scope.gameAds = gameAds;
});
}
})
}());
and here is the FinMyLocation Service:
//http://ngcordova.com/docs/#Geolocation
"use strict";
(function () {
angular.module('pokerGameFinder')
.factory('findmylocation', function($log, $cordovaGeolocation, $q, $ionicPlatform, LoaderService, AnalyticsTracker, toastr) {
return {
getCurrentLocation: getCurrentLocation
};
function getCurrentLocation(showErrors) {
//http://andyshora.com/promises-angularjs-explained-as-cartoon.html
//default to show errors if it is not passed in
showErrors = typeof showErrors !== 'undefined' ? showErrors : true;
LoaderService.show();
var deferred = $q.defer();
//does this need to be wrapped in a try catch in case it is not rwady?!?! try()
$cordovaGeolocation.getCurrentPosition()
.then(function (position) {
var addressText = "Current Location";
var currentLocation = null;
//get the address for these coords
//http://www.raymondcamden.com/2013/3/5/Simple-Reverse-Geocoding-Example
//http://www.jasonwatmore.com/post/2014/02/15/AngularJS-Reverse-Geocoding-Directive.aspx
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
addressText = results[1].formatted_address;
} else {
//don't do anything
if (showErrors) {
toastr.show('Address not found - please try again');
}
}
} else {
if (showErrors) {
$log.debug('Address Geocoder failed due to: ' + status);
toastr.show('Address not found - please try again');
}
}
currentLocation = { searchTerm: addressText, latitude: position.coords.latitude, longitude: position.coords.longitude, milesRadius: 25 }
$log.debug('Found current location', currentLocation);
AnalyticsTracker.event('findmylocation.getCurrentLocation');
LoaderService.hide();
deferred.resolve(currentLocation);
});
}, function(error) {
$log.debug('getCurrentLocation', error);
LoaderService.hide();
AnalyticsTracker.logError(error);
if (showErrors) {
toastr.show('Could not find your location - is your GPS on?');
}
deferred.reject(error);
});
return deferred.promise;
}
});
}());