Finding Closest Points to a certain Point Given its Coordinates and Maximum Distance - Query Result Undefined using Mongoose with MEAN Stack

前端 未结 2 1494
名媛妹妹
名媛妹妹 2020-12-15 22:29

I\'ve an issue that I wasn\'t able to solve in some days, even looking at related Stack Overflow Q/A.

I\'m developing an application reusing Scotch\'s Create a MEAN

相关标签:
2条回答
  • 2020-12-15 23:14

    I don't understand what's beneath all your code, but I know a thing:
    If you are using Google's Radar Search, you must take into consideration that

    The maximum allowed radius is 50 000 meters.

    Just take a look at their Documentation

    Meaning that if you try with higher radius, results could be Zero

    0 讨论(0)
  • 2020-12-15 23:19

    I finally managed to solve this issue.

    Essentially, the issue was caused by the schema, since 2dIndex was referred to a wrong field (type and coordinates).

    I solved using the following Schema:

    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 time
    geoObjects.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);
    

    And the following Query:

    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);
            });
        });
    

    I hope it will help someone!

    EDIT

    The schema I put over causes a bit of problems with LineStrings and Polygons.

    Here are correct schemas which allow using 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 time
    linestrings.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);
    

    polygon-model.js

    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 time
    polygons.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 Insert:

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

    Polygon Insert:

    {  
        "name" : "Poly", 
        "geo" : {
            "type" : "Polygon", 
            "coordinates" :  [
                               [ 
                                 [25.774, -80.190], [18.466, -66.118], 
                                 [32.321, -64.757], [25.774, -80.190] 
                               ]
                             ]
        }
    }
    
    0 讨论(0)
提交回复
热议问题