partiz
partiz

Reputation: 1216

getCurrentPosition does not work with angularjs

I try this (for get current user location when the page loads)

angular.module('ionicApp', ['ionic'])
.controller('MyCtrl', function($scope){
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position){
      $scope.$apply(function(){
        $scope.position = position;
      });
    });
  }
});

and then for print:

{{position.coords.latitude}}

it just prints: position.coords.latitude

I am using ionic-framework. Why does my code not work?

Upvotes: 0

Views: 1255

Answers (1)

Anurag Pandey
Anurag Pandey

Reputation: 744

Hi its work for me==========

if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
 console.log("Geolocation is not supported by this browser.");
}
    var latLng = {};

  function showPosition(position) {
     latLng = {
     'lat': position.coords.latitude,
     'lng': position.coords.longitude
  };
   $scope.location_address = latLng;
  }

in HTML Side by print {{location_address}}

your above code in ionic problem to not accessing direct value without declaration.

Upvotes: 3

Related Questions