1

I am using Google Maps to display locations stored in a MySQL database containing name, latitude and longitude information. An AJAX request is sent and the map is updated with all the locations. So far, so good.

Users have the ability to filter the locations by drawing polylines on the map to create a bounding box. The idea is that only locations within this bounding box are returned and they can draw whatever shape they want.

All the points used to draw the shape are returned a list of coordinates. Where I am struggling is taking the list of coordinates and finding everything within them. Can I achieve this with an SQL query? Is it even possible?

I have seen examples of returning locations within x radius from one set of coordinates and also a square box, but I have no idea how to take that further and get everything within a potentially infinite set of coordinates.

1 Answers1

0

You should transform the shape the user enters into a polygon. Then you transform all locations within a square around this polygon into the same coordinate system.

Using a solution describe in the wikipedia page for point-in-polygon problems you will be able to find all locations within this shape and remap them to coordinates in your sql database and then into locations.

There are however similar question which might help you find a proper solution to your problem. See:

How can I determine whether a 2D Point is within a Polygon?

Point in Polygon Algorithm

Community
  • 1
  • 1
cb0
  • 8,415
  • 9
  • 52
  • 80