Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Are you using the spatial extensions? – Mike T Mar 13 '11 at 23:22

Using this example with Spatial Extensions for MySQL:

SET @bounds = 'Polygon((30000 15000, 31000 15000, 31000 16000, 30000 16000, 30000 15000))';

FROM polygons
WHERE MBRContains(GeomFromText(@bounds), geom);
share|improve this 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));
share|improve this answer
Nobody should really use MySQL anyway: – jkj Mar 13 '11 at 16:51

You could use GeoServer (or MapServer) to display your polygons as a WMS layer using OpenLayers to display it over the Google Maps Basemap - then you can use WMS GetFeatureInfo or a WFS GetFeature request to query the database. As jkj says you probably want to use a proper spatial data base like postgis too.

share|improve this answer

You can use MySQL though you need to use php to generate xml for it to be passed though v3 api. A good tutorial - your need to modify it for your requirements (though again recommend postgres for gis functions is faster)

share|improve this answer

You could load your data into fusion tables and overlay the fusionlayer over the basemap. There are restrictions, that change as they improve the product so best to check

Is full accuracy of you polygon settings required, perhaps you could simplify before any migrations.

share|improve this answer

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

to get the N,E,S,W-bounds, use following:

google.maps.event.addListener(map, 'tilesloaded', function() {
    var bounds = map.getBounds(),
        boundsNeLatLng = bounds.getNorthEast(),
        boundsSwLatLng = bounds.getSouthWest(),
        zoom = map.getZoom(),
        boundaries = {
            lonE: boundsNeLatLng.lng(),
            lonW: boundsSwLatLng.lng()
        …now using boundaries.latN, etc. here via AJAX

to get all points in that viewport via MySQL:

-- and within MySQL:
SELECT `id` FROM `polylines`
WHERE :latN > `latitude` AND `latitude` > :latS
AND :lonE > `longitude` AND `longitude` > :lonW
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.