我是新来的意思是堆栈(和一般的编程),但有一个简单的应用程序启动并运行。
我的模型如下所示:
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”,然后想搜索距用户最大距离内的餐馆数据库。
我如何在这个脚手架中做到这一点?我试图让这些示例/教程起作用,但无济于事:(