Is it possible to query a MySQL database using the results from map.getBounds() with Google Maps API v3?
I have a MySQL database that con开发者_JAVA技巧tains 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?
The format of the query will depend on the database schema, of course. If you'd like to fetch polygon data for polygons with center points inside the current viewport, you could try this:
select *
from polygons
where ((lat_northeast - lat_southwest) / 2) > $viewport_lat_southwest
and ((lat_northeast - lat_southwest) / 2) < $viewport_lat_northeast
and ((lng_northeast - lng_southwest) / 2) > $viewport_lng_southwest
and ((lng_northeast - lng_southwest) / 2) < $viewport_lng_northeast
The query considers the center point to be the center of the polygon's rectangular bounding box, which could be somewhat different from the "weighted" center for peculiarly shaped polygons. The coordinates of the bounding box, as returned by calling getNorthEast() and getSouthWest() on a LatLngBounds object, are represented by the pseudo-variables "$viewport_lat_southwest", etc.; "lat_northeast" etc. represent columns in the database table.
The query can be fairly easily adapted to select only polygons having all or any of their bounding box within the viewport by comparing only the "near" coordinate (for all) or the "far" coordinate (for any) of the polygon to the viewport coordinate.
If you have separate records for the individual points in each polygon, you could select them like this:
select distinct polygon_id
from points
where lat > $viewport_lat_southwest
and lat < $viewport_lat_northeast
and lng > $viewport_lng_southwest
and lng < $viewport_lng_northeast
That would select polygons with any points in the viewport. To find polygons with all points in the viewport, you could try this:
select polygon_id
from points
where min(lat) > $viewport_lat_southwest
and max(lat) < $viewport_lat_northeast
and min(lng) > $viewport_lng_southwest
and max(lng) < $viewport_lng_northeast
group by polygon_id
That query also demonstrates a strategy for calculating the bounding box from a set of points:
select
min(lat) as latSW,
min(lng) as lngSW,
max(lat) as latNE,
max(lng) as lngNE
from points
group by polygon_id
Not entirely certain but a radius search or proximity search might be what works. You could get the bounds of the map in JavaScript, send back a query to your server with those values. The server would then query your database for all polygons that lie within that bounded region (assuming your polygons are stored with the lat-lng pairs for all their vertices).
These might be helpful (you probably have to modify the queries since they're working with points rather than polygons and you might want an incomplete polygon to show up even if it doesn't completely lie in the view - but if you're willing and able to modify your database to store the centroid of each polygon, you should be able to do this with minimal effort).
- http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
- http://www.goondocks.com/blog/08-01-22/zip_code_radius_search_using_mysql.aspx
- http://spinczyk.net/blog/2009/10/04/radius-search-with-google-maps-and-mysql/
- https://code.google.com/apis/maps/articles/phpsqlsearch.html
Calculating the centroid
- Determine the centroid of multiple points
- https://gis.stackexchange.com/questions/2128/algorithm-for-finding-irrregular-polygon-centroid-label-point
Most likely you want a space-filling-curve. I've used it for a Postcode-query or for the travelsalesman problem. The advantage of a sfc is that it reduce a 2D problem to a 1D problem thus querying its more simple. You want to look at my implementation of a sfc at phpclasses.org (hilbert-curve).
精彩评论