下面的函数创建了一个4个点的多边形正方形,我假设最后的第5个点关闭了平方,我需要它至少有24个点并构成一个圆。有什么想法吗?
function getRadius($point="POINT(-29.8368 30.9096)", $radius=2)
{
$km = 0.009;
$center = "GeomFromText('$point')";
$radius = $radius*$km;
$bbox = "CONCAT('POLYGON((',
X($center) - $radius, ' ', Y($center) - $radius, ',',
X($center) + $radius, ' ', Y($center) - $radius, ',',
X($center) + $radius, ' ', Y($center) + $radius, ',',
X($center) - $radius, ' ', Y($center) + $radius, ',',
X($center) - $radius, ' ', Y($center) - $radius, '
))')";
$query = $this->db->query("
SELECT id, AsText(latLng) AS latLng, (SQRT(POW( ABS( X(latLng) - X({$center})), 2) + POW( ABS(Y(latLng) - Y({$center})), 2 )))/0.009 AS distance
FROM crime_listing
WHERE Intersects( latLng, GeomFromText($bbox) )
AND SQRT(POW( ABS( X(latLng) - X({$center})), 2) + POW( ABS(Y(latLng) - Y({$center})), 2 )) < $radius
ORDER BY distance
");
if($query->num_rows()>0){
return($query->result());
}else{
return false;
}
}
在js版本下方,这是完美的
var findCirclePolygons = function(point, r)
{
var d2r = Math.PI / 180;
this.circleLatLngs = new Array();
numPoints = 24;
var circleLat = r * 0.009; // Convert degrees into km
var circleLng = circleLat / Math.cos(point.lat() * d2r);
for (var i = 0; i < numPoints + 1; i++) {
var theta = Math.PI * (i / (numPoints / 2));
var vertexLat = point.lat() + (circleLat * Math.sin(theta));
var vertexLng = parseFloat(point.lng()) + parseFloat(( circleLng * Math.cos(theta)));
var vertextLatLng = new google.maps.LatLng(vertexLat, vertexLng);
this.circleLatLngs.push(vertextLatLng);
}
// Set options
var options = {
paths: circleLatLngs,
strokeColor: "#0055ff",
strokeOpacity: 1,
strokeWeight: 1,
fillColor: "#0055ff",
fillOpacity: 0.35
};
// Return
return options;
};
答案 0 :(得分:2)
你可以创建一个循环(伪代码):
for(i=0; i <= 360; i += 360/24)
{
$extra_point = "POINT(". $radius*cos(i)." ". $radius*sin(i) . ")"
}
修改强>
第二次编辑 在一些较少的伪代码中;)(已经很久我没有使用php了。)
$lon = 42;
$lat = 2;
$radius = 0.01;
$bbox = "POLYGON((";
for(i=0; i <= 360; i += 360/24)
{
$bbox .= $radius*cos(deg2rad(i)) + $lon." ". $radius*sin(deg2rad(i)) + $lat;
if(i < 360)"
$bbox .= ", "
}
$bbox .= "))"
关于距离事物,你有任何x的pow(abs(x),2)== pow(x,2)。所以你可以写(为简单起见):
SQRT(POW( X(latLng) - X({$center}), 2) + POW(Y(latLng) - Y({$center}), 2 )) < $radius
甚至可以省去平方根的计算(在这种情况下,它不会改变任何事情,早期优化是一件坏事,所以你也要这样做)
POW( X(latLng) - X({$center}), 2) + POW(Y(latLng) - Y({$center}), 2 ) < POW($radius, 2)