Home > front end >  MongoDB: Geospatial Queries : Oval Shape Forming instead of circle
MongoDB: Geospatial Queries : Oval Shape Forming instead of circle

Time:08-05

im having a problem with my geospatial queries in mongoDB. In my application, i want to use a query to show all points with a max distance from my centerpoint. In my applicatoin I have a react-leaflet map where i show all query results on the map with their location. My query is working, but its showing an oval shape instead of a circle, like i would expect it to. To illustrate my point i got three images - the first are all data points, the others would be the results i get from my max distance query. I appreciate every answers / hints that can help me to properly query for the results that fit in the requested diameter.

$nearSphere: {
                    "$geometry": {
                        "type" : "Point",
                        "coordinates" :targetCoordinates
                    },
                    $maxDistance: activityRadius*1000}},

Here are the images all results

with radius

CodePudding user response:

Probably you switched latitude and longitude in mongo database.

Mongo needs location with longitude first, so [long, lat] and leaflet defines the markers the other way around [lat, long]. So to get it to work you need to switch location points in mongodb. After that latitude calculation should be right, so you don't get an oval as a result.

Mongo specification: https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/

  • Related