栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在给定坐标和最大距离的情况下找到最接近某个点的点-使用带有MEAN Stack的Mongoose查询结果不确定

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在给定坐标和最大距离的情况下找到最接近某个点的点-使用带有MEAN Stack的Mongoose查询结果不确定

我终于设法解决了这个问题。

本质上,此问题是由模式引起的,因为

2dIndex
它被指向错误的字段
(type and coordinates)

我使用以下 模式 解决了:

var mongoose = require('mongoose');var GeoJSON  = require('geojson');var Schema   = mongoose.Schema;var geoObjects = new Schema({         name : {type: String},         type: {      type: String,      enum: [   "Point",   "LineString",   "Polygon" ]     },          coordinates: [Number],          created_at: {type: Date, default: Date.now},          updated_at: {type: Date, default: Date.now}});// Sets the created_at parameter equal to the current timegeoObjects.pre('save', function(next){   now = new Date();   this.updated_at = now;   if(!this.created_at) {      this.created_at = now   }   next();});geoObjects.index({coordinates: '2dsphere'});module.exports = mongoose.model('geoObjects', geoObjects);

和以下 查询

app.post('/query/', function(req, res) {        // Grab all of the query parameters from the body.        var lat = req.body.latitude;        var long = req.body.longitude;        var distance = req.body.distance;        var query = GeoObjects.find({'type':'Point'});        // ...include filter by Max Distance         if (distance) { // Using MongoDB's geospatial querying features.  query = query.where('coordinates').near({     center: {         type: 'Point',         coordinates: [lat, long]     },     // Converting meters to miles     maxDistance: distance * 1609.34,     spherical: true });        }        // Execute Query and Return the Query Results        query.exec(function(err, geoObjects) { if (err)     res.send(err); // If no errors, respond with a JSON  res.json(geoObjects);        });    });

希望对您有所帮助!

编辑

我提出的模式会给

LineStrings
and 带来一些问题
Polygons

这是允许使用的正确架构

geoQueries

linestring-model.js:

var mongoose = require('mongoose');var Schema   = mongoose.Schema;// Creates a LineString Schema.var linestrings = new Schema({    name: {type: String, required : true},    geo : {        type : {type: String, default: "LineString"},        coordinates : Array    },    created_at: {type: Date, default: Date.now},    updated_at: {type: Date, default: Date.now}});// Sets the created_at parameter equal to the current timelinestrings.pre('save', function(next){    now = new Date();    this.updated_at = now;    if(!this.created_at) {        this.created_at = now    }    next();});linestrings.index({geo : '2dsphere'});module.exports = mongoose.model('linestrings', linestrings);

多边形模型

var mongoose = require('mongoose');var Schema   = mongoose.Schema;// Creates a Polygon Schema.var polygons = new Schema({    name: {type: String, required : true},    geo : {        type : {type: String, default: "Polygon"},        coordinates : Array    },    created_at: {type: Date, default: Date.now},    updated_at: {type: Date, default: Date.now}});// Sets the created_at parameter equal to the current timepolygons.pre('save', function(next){    now = new Date();    this.updated_at = now;    if(!this.created_at) {        this.created_at = now    }    next();});polygons.index({geo : '2dsphere'});module.exports = mongoose.model('polygons', polygons);

LineString插入

{      "name" : "myLinestring",     "geo" : {        "type" : "LineString",         "coordinates" : [ [     17.811,      12.634 ],  [     12.039,      18.962 ],  [     15.039,      18.962 ],  [     29.039,      18.962 ]        ]    }}

多边形插入:

{      "name" : "Poly",     "geo" : {        "type" : "Polygon",         "coordinates" :  [     [        [25.774, -80.190], [18.466, -66.118],        [32.321, -64.757], [25.774, -80.190]      ]   ]    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484931.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号