Cordova each plug-in use Introduction series (v)-$cordovaGeolocation get the current location

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.