在谷歌地图v3中通过绘制多边形在所选区域上搜索

时间:2014-10-05 08:47:54

标签: mysql database google-maps google-maps-api-3

如何在数据库中搜索最小,最大纬度和经度,在谷歌地图中绘制一个带有多边形,矩形,圆形的空间区域?
我用它来通过用户绘制地图:

drawingManager = new google.maps.drawing.DrawingManager (
{
    drawingMode: google.maps.drawing.OverlayType.POLYGON,
    drawingControlOptions:
    {
        position: google.maps.ControlPosition.TOP_LEFT,
        drawingModes: [google.maps.drawing.OverlayType.CIRCLE]
    },
    polygonOptions: polyOptions,
    rectangleOptions: polyOptions,
    circleOptions: polyOptions,
    map: map
});

所以,我想查询数据库以获取所选区域中存在的信息 绘图选项是:
1- google.maps.drawing.OverlayType.POLYGON
2- google.maps.drawing.OverlayType.RECTANGLE
3- google.maps.drawing.OverlayType.CIRCLE

任何可以对我有帮助吗?

1 个答案:

答案 0 :(得分:2)

编号2 first.Rectangle使用边界框$ minLat,$ maxLat,$ minLng,$ minLat

$sql ="SELCT * FROM tablename WHERE(lat  BETWEEN ? AND ? )AND (lng  BETWEEN ? AND ?)";
    $stmt = $dbh->prepare($sql);
    // Assign parameters
    $stmt->bindParam(1,$minLat);
    $stmt->bindParam(2,$maxLat);
    $stmt->bindParam(3,$minLng);
    $stmt->bindParam(4,$maxLng);

数字1多边形使用矩形的边界框然后javascript pointInPolygon()来消除点的多边形。

function pointInPolygon(polySides,polyX,polyY,x,y) {
 var j = polySides-1 ;
  oddNodes = 0;
  for (i=0; i<polySides; i++) {
    if (polyY[i]<y && polyY[j]>=y  ||  polyY[j]<y && polyY[i]>=y) {
        if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x)  {
            oddNodes=!oddNodes; 
        }
    }
   j=i; }

  return oddNodes;
}   

3号圈

$sql ="SELECT  *, ( 3959 * acos( cos( radians(?) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians($) ) * sin( radians( lat ) ) ) ) AS distance FROM tablename HAVING radius < 2 ORDER BY distance "(;
     $stmt = $dbh->prepare($sql);
    // Assign parameters
    $stmt->bindParam(1,$center_lat);
    $stmt->bindParam(2,$center_lng);
    $stmt->bindParam(3,$center_lat);
    $stmt->bindParam(4,$radius);