'Get all coords within radius in google maps

The thing is I'm trying to query my database to find all points that fall within a radius of a certain given point. One possible way of doing this would be to get all the data on the database and then find the distance between those points and the point I'm interested in, but I have 36k records in that database and that would mean 36k google maps requests, which I understand wouldn't be possible with the free API.

What I was thinking is getting all possible coords in a radius of that point I'm interested in and check if any of the points in the database match those coords. Is that even possible? Or efficient? I suppose I would get a LOT of points and it would translate into a very long for loop, but I can't think of any other way.

Any suggestions?

EDIT

Ok, I'll give a little more detail as of the specific scenario as I forgot to do so and now several other problems came to my mind.

First off I'm using mongodb as a database. Secondly, my database has the locations in UTM format (this is supposed to work only in zone 21s) whereas I'm handling client side coords with Google Map's LatLng coords. I'm converting the UTM coords to LatLng to actually use them in the map.

Haversine won't do it in this scenario would it?



Solution 1:[1]

Look into using the Haversine formula - if you have latitude and longitude in your database then you can use the Haversine formula in a SQL query to find records within a certain distance.

This article covers it with more details: http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/

Solution 2:[2]

Do you have latitude and longitude as separate numerical fields in your database? You could search for all points in your database that are in a square area whose sides are twice the radius of the circle you're looking for. Then just check the distances within that subset of points.

 select * from points where lat > my_latitude-radius and lat < my_latitude + radius and long > my_longitude-radius and long < my_longitude+radius;

Solution 3:[3]

Here I have done with mongoose. We need to create a schema that contain type and coordinates. You can see more details in https://mongoosejs.com/docs/geojson.html

So it's has another problem with mongoose version. Mongoose v6.3.0 worked for me. When you will use countDocuments with the query, it can be generate error but count function not generating any error. I know count deprecated, it shouldn't be use but I haven't find better solution. If anyone find solution for count, let me know. Also you can visit https://github.com/Automattic/mongoose/issues/6981

const schema = new mongoose.Schema(
  {
    location: {
      type: {
        type: String,
        enum: ["Point"],
      },
      coordinates: {
        type: [Number],
        index: "2dsphere",
      },
    },
  },
  { timestamps: true }
);

const MyModel = mongoose.model("rent", schema);

The query will be

const result = await MyModel.find({
      location: {
        $near: {
          $geometry: {
            type: "Point",
            coordinates: [Number(filters.longitude), Number(filters.latitude)],
          },
          $maxDistance: filters.maxRadius * 1000,
          $minDistance: filters.minRadius * 1000,

        },
      },
    })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cody Caughlan
Solution 2 Chris Bogart
Solution 3 Tofazzal haque