[GIS] Is it possible to query a MySQL database using the results from map.getBounds() with Google Maps API v3

centroidsgoogle mapsjavascriptMySQL

I have a MySQL database that contains a large number of polygons, one for each district on a map. They are all quite complicated and it's unrealistic to simply load them all at once. I've looked into using MarkerManager for Google Maps API v3 and this seems to be working as intended. However I'm not sure how to best go about querying my database for polygons that are in the viewport.

I'm thinking that it would be best to store the center of each polygon in a separate column as a simple lat,lng pair so that a query could be run on that – and then return the full polygon.

What is the best way to go about this?

SELECT * FROM polygons WHERE [the center of each polygon is within the bounds of the viewport]

Is this even possible? Am I going about this the wrong way?

And additionally, is there a simple script I can run on my data to return the center of each polygon?

Best Answer

You could migrate your database to PostgreSQL and use PostGIS.

Make sure you have GiST index covering you polygon shape column and use && operator to find the polygons intersecting with the viewport

SELECT * FROM polygons WHERE geometry && ST_MakeBox2D(ST_Point(x1, y1), ST_Point(x2,y2));