平均堆栈2dsphere半径搜索

时间:2016-01-31 20:17:24

标签: mongodb mongoose mean-stack 2dsphere

我是新手,意思是堆栈(和一般的编程),但有一个简单的应用程序启动并运行。

我的模型看起来像这样:

var mongoose = require('bluebird').promisifyAll(require('mongoose'));

var RestaurantSchema = new mongoose.Schema({
  name: String,
  street: String,
  zip: Boolean,
  city: String,
  country: String,
  location: {
    coordinates: {type: [Number], index: '2dsphere'}
  },
  kitchen: String,
  rating: Number
});

RestaurantSchema.index({location: '2dsphere'});

export default mongoose.model('Restaurant', RestaurantSchema);

我通过以下方式播种数据库:

Restaurant.find({}).removeAsync()
  .then(() => {
    Restaurant.create({
      name: 'Kaspers restaurant',
      street: 'Nørrebrogade 1',
      zip: '2200',
      city: 'Copenhagen',
      country: 'Denmark',
      location: ['12.548057','55.7034'],
      kitchen: 'Italian',
      rating: '4.5'
    }, {
      name: 'Kristinas restaurant',
      street: 'Østerbrogade 2',
      zip: '2200',
      city: 'Copenhagen',
      country: 'Denmark',
      location: ['12.541565', '55.689206'],
      kitchen: 'Thai',
      rating: '5'
    }, {
      name: 'Noma',
      street: 'Strandgade 93',
      zip: '1401',
      city: 'Copenhagen',
      country: 'Denmark',
      location: ['12.59620', '55.677933'],
      kitchen: 'Nordisk',
      rating: '6'
    });
  });

我的控制器通过以下方式提取用户位置和列出餐馆:

angular.module('venueApp')
  .controller('RestaurantsCtrl', function ($scope, $http, $window) {
      $http.get('/api/restaurants')
          .success(function(data) {
            $scope.restaurants = data;
            console.log($scope.restaurants);
          })
          .error(function(err) {
            console.log('Error! Something went wrong');
          });

      $window.navigator.geolocation.getCurrentPosition(function(position) {
      var lat = position.coords.latitude;
      var lng = position.coords.longitude;

        $scope.$apply(function () {
          $scope.lat = lat;
          $scope.lng = lng;
        });
      });




  });

我的视图显示用户位置,获取最大输入并通过以下方式列出餐馆:

<navbar></navbar>
<div>
    This is your location:
    <input type="text" class="disabled" placeholder="{{lat}}">
    <input type="text" class="disabled" placeholder="{{lng}}">
    <br>
    How far do you want to search:
    <div class="form-group">
        <label for="distance">Max. Distance (miles)</label>
        <input type="text" class="form-control" id="distance" placeholder="500" ng-model="formData.distance">
    </div>


</div>
<div class="col-md-12">
    <table class="table table-striped">
        <thead>
        <th>Navn</th>
        <th>By</th>
        <th>Køkken</th>
        <th>Latitude</th>
        <th>Longtitude</th>
        </thead>
        <tbody>
            <tr ng-repeat="restaurant in restaurants">
                <td>{{restaurant.name}}</td>
                <td>{{restaurant.city}}</td>
                <td>{{restaurant.kitchen}}</td>
                <td>{{restaurant.location.coordinates[0]}}</td>
                <td>{{restaurant.location.coordinates[1]}}</td>
            </tr>
        </tbody>
    </table>

我使用过yeoman angular-fullstack发生器。在我的html视图中,我提取用户当前位置“lat”和“lng”,然后想要搜索距用户最远距离的餐馆数据库。

我如何在这个脚手架中做到这一点? 我试图让这些示例/教程起作用,但无济于事:(

0 个答案:

没有答案