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 the 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 for getting 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 value $scope. currentposition = { Latitude:location.latitude, longitude:location.longitude };});
Just call the GetCurrentPosition () function, and you can always monitor the change in location data.
Cordova each plug-in use Introduction series (v)-$cordovaGeolocation get the current location