Ionic show latitude and longitude in view

1

I'm not able to display my location in the view, but using console.log (long) I can see from the console my location. Where can I be going wrong? ' .controller ('GeoCtrl', function ($ scope, $ cordovaGeolocation) {

var posOptions = {
    timeout: 10000,
    enableHighAccuracy: true
};
$cordovaGeolocation
    .getCurrentPosition(posOptions)
    .then(function (position) {
        var lat = position.coords.latitude
        var long = position.coords.longitude
        console.log(long);
        $scope.latitude = lat;
        $scope.longitude = long;
    }, function (err) {
        // error
    });


var watchOptions = {
    timeout: 3000,
    enableHighAccuracy: false // may cause errors if true
};

var watch = $cordovaGeolocation.watchPosition(watchOptions);
watch.then(
    null,
    function (err) {
        // error
    },
    function (position) {
        var lat = position.coords.latitude
        var long = position.coords.longitude
        $scope.latitude = lat;
        $scope.longitude = long;
    });


watch.clearWatch();
// OR
$cordovaGeolocation.clearWatch(watch)
    .then(function (result) {
        // success
    }, function (error) {
        // error
    });

});

<ion-pane>
    <ion-header-bar class="bar-stable">
        <h1 class="title">Geolocalização</h1>
    </ion-header-bar>
    <ion-content ng-controller="GeoCtrl">
        {{latitude}},{{longitude}}
    </ion-content>
</ion-pane>
    
asked by anonymous 13.09.2016 / 16:51

1 answer

1

I solved the problem, I was wrong with the code I took based on the ngCordova website, I removed the part that was wrong because I was not using it, so the controller was like this.

.controller('GeoCtrl', function ($scope, $cordovaGeolocation) {

var posOptions = {
    timeout: 10000,
    enableHighAccuracy: false
};
$cordovaGeolocation
    .getCurrentPosition(posOptions)
    .then(function (position) {
        var lat = position.coords.latitude
        var long = position.coords.longitude
        $scope.latitude = lat;
        $scope.longitude = long;
        alert($scope.latitude);
        alert($scope.longitude);


    }, function (err) {
        // error
    });


var watchOptions = {
    timeout: 3000,
    enableHighAccuracy: false // may cause errors if true
};

var watch = $cordovaGeolocation.watchPosition(watchOptions);
watch.then(
    null,
    function (err) {
        // error
    },
    function (position) {
        var lat = position.coords.latitude
        var long = position.coords.longitude
        $scope.latitude = lat;
        $scope.longitude = long;
    });

});

    
13.09.2016 / 17:55