简体   繁体   English

Google Maps Circle / MySQL查询

[英]Google Maps Circle / MySQL query

I would like to draw a circle on a map, and get all rows in my database with geo the lat/lng points that fall into that circle. 我想在地图上画一个圆,并获取包含该圆经纬度的经纬度的数据库中的所有行。

I don't want draw the circle using google.maps.Circle as that applies mercator projection distortion. 我不想使用google.maps.Circle画圆,因为这会导致墨卡托投影失真。 I want a geometric rather than geographic circle. 我想要一个几何而不是地理圆。

So I can probably draw this to Google maps using an overlay of some sorts. 因此,我可能可以使用某种叠加层将其绘制到Google地图上。 But is it possible to query my database for these points? 但是可以查询我的数据库中的这些点吗?

If I were to draw a geographic circle I could use the haversine formula , but that won't work for a geometric circle. 如果要绘制地理圆,则可以使用Haversine公式 ,但不适用于几何圆。

You can use Pythagoras to find points within circle. 您可以使用毕达哥拉斯找到圆内的点。

a^2 + b^2 = c^2

The following SQL uses PDO to find points within the radius of circle 以下SQL使用PDO查找圆半径内的点

$stmt = $dbh->prepare("SELECT  name, lat, lng, ( SQRT(POW(? - lat, 2)
                      + POW(? - lng, 2))) AS distance FROM mytable   
                       HAVING distance < ? ORDER BY distance ASC ");
    // Assign parameters
    $stmt->bindParam(1,$center_lat);//Center of circle
    $stmt->bindParam(2,$center_lng);////Center of circle
    $stmt->bindParam(3,$radius);

Javascript function if required JavaScript函数(如果需要)

function Pythagoras (x1,y1,x2,y2){
    var Dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
return Dist ;
}

You can draw the circle using a polygon with enough segments to look ok: 您可以使用具有足够线段的多边形来绘制圆形,以使其看起来正常:

 // lat, lng, and radius specify the circle // CosDeg, SinDeg are functions that accept input in degrees mPerDegLat = 110540, mPerDegLng = CosDeg(lat) * 111320; //-- mPerDegLng CORRECTED FOR LATITUDE numSegs = 64; deltaAng = 360 / numSegs; verts = []; for (i=0; i<numSegs; i++) { angle = i * deltaAng; dy = CosDeg(angle) * radius; dx = SinDeg(angle) * radius; deltaLat = dy / mPerDegLat; //-- METRES PER DEGREE deltaLng = dx / mPerDegLng; vertex = {lat:(lat + deltaLat), lng:(lng + deltaLng)}; verts.push(vertex); } polyCircle = new google.maps.Polygon({paths:verts, strokeColor...}); 

As for MySql, could you just run your query for a "geographically rectangular" area using lat, lng values with enough padding to make sure you don't miss any, and then filter the results with a script that does the inverse calculation to see if they are inside the circle? 至于MySql,您是否可以使用纬度,经纬度值(带有足够的填充量)来确保对“地理矩形”区域运行查询,以确保您不会遗漏任何内容,然后使用执行逆计算的脚本过滤结果以查看他们是否在圈子内?

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

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