0

私はレコードとユーザーの位置の距離を取得しようとしています。私に一番近いもので注文してください。 私はハセサインに似た式を使用していますが、まだ距離はありません。ありがとう。anglejsで距離を取得して距離でソートする方法は?

私のコードのhtml:半正矢と同様

<div ng-app="my-app" id="por_logo.html">  
     <ons-page ng-controller="PorlogoCtl"> 
     <div class="cliente" ng-repeat="cliente in porlogo.clientes" ng-if="cliente.estado == '1'"> 
     <p>{{cliente.nombre}} <span>{{distancia2}}</span></p> 
     </div>  
</ons-page> 
</div> 

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { 
      var R = 6878; // Radius of the earth in km 
      var dLat = deg2rad(lat2-lat1); // deg2rad below 
      var dLon = deg2rad(lon2-lon1); 
      var a = 
      Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
      Math.sin(dLon/2) * Math.sin(dLon/2) 
      ; 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c; // Distance in km 
      return d; 
     }; 
     function deg2rad(deg) { 
      return deg * (Math.PI/180) 
     }; 

コードJS:

var app = angular.module('my-app', ['onsen']).factory('position', function($rootScope){ 

     // console.log('building position') 
     var position = {}; 

      // 1ST/AUTO GEOLOCATION OF USER 
      // displays a popup to indicate current user location - (disabled) 
      // onSuccess Callback - This method accepts a Position object, which contains the current GPS coordinates 
     var onSuccess = function(position2) { 

       console.log(position2.coords.latitude) 
       console.log(position2.coords.longitude) 

       position.latitude = position2.coords.latitude; 
       position.longitude = position2.coords.longitude; 
       $rootScope.$digest() 
      }; 

     function onError(error) { // onError Callback receives a PositionError object 
      // alert('code: ' + error.code + '\n' + 
        // 'message: ' + error.message + '\n'); 
        alert('No podemos acceder a su ubicación'); 
     } 

     navigator.geolocation.getCurrentPosition(onSuccess, onError); 

     return position; 

    }); 
    app.controller("PorlogoCtl", function($scope, $http, position, $rootScope) { 
     $http.get('https://viveenunclick.com/api.php/clientes?transform=1'). 
     success(function(data, status, headers, config) { 
     $scope.porlogo = data; 
     $rootScope.latitude = position.latitude; 
     $rootScope.longitud = position.longitude; 
     $rootScope.distancia2 = getDistanceFromLatLonInKm(position.latitude,position.longitude,$rootScope.latitude,$rootScope.longitud).toFixed(1); 
     console.log($rootScope.longitud); 
     }). 
     error(function(data, status, headers, config) {}); 
    }); 

Post Demo in Codepen

+0

を、この機能を使用することができますか? –

答えて

0

は座標が返されたデータであり、あなたは$scope.porlogo[iteration].Latitud & $scope.porlogo[iteration].Longitud

このように、論文を取得するには、反復処理が必要です。

重要::右の距離を返すために、このdistancia機能を追加し、

<p>{{cliente.nombre}} <span>{{distancia(cliente)}}</span></p> 

とあなたのコントローラに:あなたは、ビューの反復を使用することができしかし、あなたが使用する必要がある場合toFixed(1)は文字列を返します(注文は文字列で行います)。parseFloat()を追加すると数値が返され、注文は正しくなります。

$scope.distancia = function(item) { 
    //it's item.Latitud & item.Longitud, from the data 
    return parseFloat(getDistanceFromLatLonInKm(position.latitude,position.longitude,item.Latitud,item.Longitud).toFixed(1)); 
} 

あなたはどのようなデータベースを使用している、ソート順&すぎ

<div class="cliente" ng-repeat="cliente in porlogo.clientes | orderBy: distancia: false"> 

Working pen

+0

非常によく距離を取得するように思われる。しかし、注文は正しく注文しません:** 3.1,20.3,20.2、.... 19.4、1.9 ** このケースを解決する方法はありますか? –

+0

私はこのコードを見つけてそれに適応しようとしますが、成功しません。 http://jsfiddle.net/6tp92nfn/ –

+0

.toFixed(1)を削除すると正しい順序で返されます。あなたが理由を知っている? toFixed()は文字列を返します:D add parseFloatこれを修正しました(私がオフだったのは残念です) – Fetrarij

関連する問題