Ionic show lautitude and longitude in view

Asked

Viewed 142 times

1

I’m not being able to show my location in the view, but using the.log(long) console I can see my location from the console. Where can I be missing? ` .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>
  • Try putting a $scope.$apply(); within your function that brings the position, below the $scope.longitude = long

  • @joaopaulosantosalmeida is giving an error so Ionic.bundle.js:26794 Typeerror: Cannot read Property 'then' of Undefined, already went through it?

1 answer

1


I solved the problem, I was with error in the code I took as base on the site ngCordova, I removed this part that was wrong because I was not using there 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;
    });

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.