[英]How to check if markers are within circle radius without using Google Maps API, through PHP / CodeIgniter
I am using Google Maps to develop a app using Code Igniter, and I have a big issue with feature of checking if the coordonates of a marker are within the radius of a circle which is stored in my database. 我正在使用Google Maps使用Code Igniter开发应用程序,并且在检查标记的坐标是否在存储在数据库中的圆的半径内时遇到了一个大问题。
I will try to describe in pseudocode, and sorry I'm a rookie. 我将尝试用伪代码来描述,对不起,我是菜鸟。
Right click-> a circle appears on the google map with a InfoBubble where you complete name, color and circle radius in meters; 右键单击->在Google地图上会出现一个带有InfoBubble的圆圈,在其中您可以完成以米为单位的名称,颜色和圆圈半径;
Click save-> Using ajax I send the circle center, name, color, etc. 单击保存->使用ajax,我发送圆心,名称,颜色等。
In the AJAX I am storing in the database the information about the circle, but before registering I need to set the number of markers that are found within the radius of the created circle. 在AJAX中,我将有关圆的信息存储在数据库中,但是在注册之前,我需要设置在所创建圆的半径内找到的标记数。
I have searched and found this link http://www.movable-type.co.uk/scripts/latlong.html , but my math is not that great. 我已经搜索并找到此链接http://www.movable-type.co.uk/scripts/latlong.html ,但是我的数学并不是那么好。
I need a function that takes the circle, marker coords and radius and that returns true or false if the marker is within the circle or not. 我需要一个带有圆,标记坐标和半径的函数,如果标记是否在圆内,则该函数返回true或false。
I made something that I found over the net but it does not work 我做了一些我在网上找到的东西,但是没用
public function arePointsNear($checkPoint, $centerPoint, $km) {
$km = $km * 1000;
$ky = 40000 / 360;
$kx = cos(pi() * $centerPoint->lat / 180.0) * $ky;
$dx = abs($centerPoint->lng - $checkPoint->lng) * $kx;
$dy = abs($centerPoint->lat - $checkPoint->lat) * $ky;
return sqrt($dx * $dx + $dy * $dy) <= $km;
}
Thanks! 谢谢!
Let me give you code that calculates that in Javascript; 让我给您提供用Javascript计算该代码的代码; all within the Google Maps code, but the function that calculates the distance is just a function, not a service. 所有这些都在Google Maps代码中,但计算距离的函数只是一个函数,而不是服务。 (I don't know who wrote the function) (我不知道是谁写的函数)
Your question is to have a PHP function, right? 您的问题是要具有PHP函数,对吗? No doubt you can translate the javascript function to PHP; 毫无疑问,您可以将javascript函数转换为PHP; or you just trust the calculation in javascript and send that result with Ajax. 或者您只是信任javascript中的计算,然后使用Ajax发送该结果。
The code draws a Circle (center = Brussels; radius = 30km) and 4 markers. 该代码绘制了一个圆(中心=布鲁塞尔;半径= 30km)和4个标记。 You can drag them all. 您可以全部拖动它们。 Clicking on the button triggers the calculation. 单击按钮触发计算。 I show the result by turning the markers green or red. 我通过将标记变成绿色或红色来显示结果。
(Do you know how to take over from here?) (您知道如何从这里接管吗?)
<style>
#map-canvas {
height: 400px;
margin: 0px;
padding: 0px;
}
</style>
<div id="map-canvas"></div>
<input type="button" id="start" value="Click">
<p>Drag the circle, drag the markers; when you click the button it will calculate if the markers are in the circle</p>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?"></script>
<script>
function initialize() {
// settings
var center = new google.maps.LatLng(50.84546, 4.357112);
var radius_circle = 30000; // 30km
var markerPositions = [
{lat: 50.940749, lng: 4.2033035},
{lat: 50.791671, lng: 4.587825},
{lat: 50.66649, lng: 3.945124},
{lat: 50.429139, lng: 4.813044}
];
var markers=[];
// draw map
var mapOptions = {
center: center,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var circle = drawCircle(mapOptions.center, radius_circle);
// markers
for (var i=0; i<markerPositions.length; i++) {
markers.push(
new google.maps.Marker({
position: new google.maps.LatLng(markerPositions[i].lat, markerPositions[i].lng),
map: map,
draggable: true
})
);
}
// client clicks on button, we will check for the markers within the circle
google.maps.event.addDomListener(document.getElementById('start'), 'click', function() {
for (var i=0; i<markerPositions.length; i++) {
var distance = calculateDistance(
markers[i].getPosition().lat(),
markers[i].getPosition().lng(),
circle.getCenter().lat(),
circle.getCenter().lng(),
"K"
);
if (distance * 1000 < radius_circle) { // radius is in meter; distance in km
markers[i].setIcon('http://maps.gstatic.com/mapfiles/icon_green.png'); // make or find a better icon
}
else {
markers[i].setIcon('http://maps.gstatic.com/mapfiles/icon.png'); // make or find a better icon
}
}
});
function drawCircle(center, radius) {
return new google.maps.Circle({
strokeColor: '#0000FF',
strokeOpacity: 0.7,
strokeWeight: 1,
fillColor: '#0000FF',
fillOpacity: 0.15,
draggable: true,
map: map,
center: center,
radius: radius
});
}
function calculateDistance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var radlon1 = Math.PI * lon1/180;
var radlon2 = Math.PI * lon2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K") { dist = dist * 1.609344; }
if (unit=="N") { dist = dist * 0.8684; }
return dist;
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.