2017-08-18 9 views
0

私は、座標を持つ場所を格納するAPIを構築しており、ユーザーとの距離に基づいてクエリを実行したいと考えています。ユーザーの現在位置を返しますnodejs:場所からユーザーまでの距離を計算する

var geolocation = angular.module('geolocation', []);   
     navigator.geolocation.watchPosition(render); 
     var lat, lon = 0; 
     function render(pos) { 
      lat = pos.coords.latitude; 
      lon = pos.coords.longitude; 
      console.log(lat +","+ lon); 
     } 

:私のビューコントローラで

は、私はこのコードを持っています。 私のスキーマは次のようにモデル化される。

var LocationSchema = new Schema({ 
    locationName: { 
    type: String, 
    Required: 'Enter the name of the location.' 
    }, 
    description: { 
    type: String, 
    Required: 'Enter the description of the location.' 
    }, 
geolocation: { 
    type: { type: String, default:"Point" }, 
    coordinates: [Number], 
    }, 

そして、私のAPIcontrollerに、私はこれを実行しようとしました:

exports.find_all_locations_near = function(req, res) { 
    Location.find({ 
    geolocation: 
     { 
     $near : 
     { 
      $geometry: { type: "Point", coordinates: [ lat, lon ] }, 
      $minDistance: req.body.minDistance, 
      $maxDistance: req.body.maxDistance 
     } 
     } 
    }), function(err, location) { 
    if (err) 
     res.send(err); 
    else if (location =='') 
     //res.json({ message: 'No location was found.' }); 
     console.log('No location was found.'); 
    else 
     res.json(location);  
    }; 
} 

私はminとmaxの距離をこのように提出しています:

<div class="form-group">       
         <label>Min Distance</label> <input type="text" class="form-control text-center" ng-model="formData.minDistance"><br> 
        </div> 
        <div class="form-group">       
         <label>Max Distance</label> <input type="text" class="form-control text-center" ng-model="formData.maxDistance"><br> 
        </div> 

しかし、何も起こりません。私は0の結果を得る。私は間違って何をしていますか? また、ユーザーと場所の距離を計算して、各行の距離を返すにはどうすればよいですか?

答えて

関連する問題