简体   繁体   English

是否可以通过Google Maps API v3使用map.getBounds()的结果查询MySQL数据库?

[英]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 contains a large number of polygons, one for each district on a map. 我有一个MySQL数据库,其中包含大量的多边形,在地图上每个分区一个。 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. 我已经研究过将MarkerManager用于Google Maps API v3,这似乎可以正常工作。 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. 我认为最好将每个多边形的中心存储在一个单独的列中,作为简单的lat,lng对,这样就可以在该列上运行查询-然后返回完整的多边形。

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? 另外,是否有一个简单的脚本可以对数据运行以返回每个多边形的中心?

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. 您可以使用JavaScript获取地图的边界,使用这些值将查询发送回服务器。 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). 然后,服务器将在数据库中查询位于该有界区域内的所有多边形(假设您的多边形与所有顶点的lat-lng对一起存储)。

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). 这些可能会有所帮助(您可能需要修改查询,因为它们使用的是点而不是多边形,并且您可能希望显示不完整的多边形,即使该多边形不完全位于视图中也可以。但是,如果您愿意并且能够修改您的数据库以存储每个多边形的质心 ,那么您应该可以轻松完成此操作)。

Calculating the centroid 计算质心

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.; 通过在LatLngBounds对象上调用getNorthEast()和getSouthWest()返回的边界框坐标由伪变量“ $ viewport_lat_southwest”等表示; "lat_northeast" etc. represent columns in the database table. “ lat_northeast”等表示数据库表中的列。

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

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. sfc的优点在于,它可以将2D问题简化为1D问题,从而使其查询更为简单。 You want to look at my implementation of a sfc at phpclasses.org (hilbert-curve). 您想在phpclasses.org(hilbert-curve)上查看我对sfc的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM