How to define the value of a variable with a json

.controller('HomeCtrl', function($scope,  $http, $stateParams, $cordovaGeolocation ){
 
 var toRadians = function (num) {
            return num * Math.PI / 180;
        };


 console.log($stateParams.proveedoresID);


 $scope.enviar = function() {


  var lat1=5.231805;
  var lng1=-75.786768;
  var distance=1;
     
$http.get("http://www.efruver.com/u.php")


.then(function (response) {$scope.categoria = response.data.records,$scope.lat=response.data.records.lat,$scope.lng=response.data.records.lng; })
 
**var lat="$scope.lat";//Here i need the value of lat in Json**
**var lng="$scope.lng";//Here i need the value of lng in Json**


 $scope.distancia=(6371*Math.acos( Math.sin(toRadians(lat)) 
                                            * Math.sin(toRadians(lat1)) 
                                            + Math.cos(toRadians(lng -lng1)) 
                                            * Math.cos(toRadians(lat)) 
                                            * Math.cos(toRadians(lat1))
                                            ));                                         

 }


})

I need to put as value of the variables lat and lng the arrays I bring from mysql by means of php

Please, put your code inside a code box (like this)

angular.module('helloWorld').controller('hello', function() {
    function greetings() {
        console.log("Grettings!"):
    }
});

And write you question with details. Thanks :slight_smile:

1 Like

Okey. I see few errors in your code. First of all, I strongly recommend you to check Angular’s $http service docs.

In second place, check this code:

$http.get("http://www.efruver.com/ubicarproveedores.php").then(
    function (response) {
        $scope.categoria = response.data.records;
        $scope.lat = response.data.records.lat;
        $scope.lng = response.data.records.lng; 
   }
)

Notice that I’m creating a different $scope.var for every variable that I want to store into the $scope. Now we have lat and lng stored into the $scope, so we can access to their values whenever we want.

So, here we can use the following code:

var lat = $scope.lat;
var lng = $scope.lng;

 $scope.distancia=(6371*Math.acos( Math.sin(toRadians(lat)) 
                                            * Math.sin(toRadians(lat1)) 
                                            + Math.cos(toRadians(lng -lng1)) 
                                            * Math.cos(toRadians(lat)) 
                                            * Math.cos(toRadians(lat1))
                                            )); 

And voilá!

1 Like

If you can print lat and lng after assign them, the $scope are OK. So must be a problem with the values. Maybe JavaScript is not using lat1=5.231805; and lng1=-75.786768; as float.

NaN means “Not a Number”, so it means Math class cannot work.

Try casting the float values as parseFloat(var).

1 Like

Can you post the value of response.data?

Maybe it need more casting… I haven’t work with PHP and JSON since 2 years ago (at least) but I remember there was few issues with it.