For more information, see: http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-5-cordovageolocation/
$cordovaGeolocation is the Ngcordova plug-in that can get the current location, applied to it in the project, and explained here:
1, first need to download this plugin, the command is:
Cordova Plugin Add cordova-plugin-geolocation
2, in the JS code as follows, this code is written in the corresponding controller and rely on ' $cordovaGeolocation ', remember in app.js rely on ' Ngcordova ', this is Ngcordova official website controller inside the code,:
module.controller(‘GeoCtrl‘, function($cordovaGeolocation) {
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
var lat = position.coords.latitude
var long = position.coords.longitude
}, function(err) {
// error
});
});
3, in the project, I need real-time monitoring of geographical location, so the use of angularjs inside the $broadcast, $on; $broadcast can monitor the change in value, and $on is a change in the value of the monitoring, can trigger to it, With the plug-in to get the location mentioned above, you can always monitor the location changes, the code is as follows:
module.controller(‘GeoCtrl‘, function($cordovaGeolocation) {
function getCurrentPosition()
{
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
$rootScope.$broadcast(‘selfLocation:update‘, position);
var lat = position.coords.latitude
var long = position.coords.longitude
}, function(err) {
// error
});
});
In other controllers that need to get position, add $on:
$scope.$on(‘selfLocation:update‘, function (_, location) {
// Constantly updated values
$scope.currentPosition = {
Latitude: location.latitude,
Longitude: location.longitude
};
});
Just call the GetCurrentPosition () function, and you can always monitor the change in location data.
This article is from the "11141997" blog, please be sure to keep this source http://11151997.blog.51cto.com/11141997/1752538
Cordova each plug-in use Introduction series (v)-$cordovaGeolocation get the current location