【问题标题】:How to check if markers are within circle radius without using Google Maps API, through PHP / CodeIgniter如何通过 PHP / CodeIgniter 在不使用 Google Maps API 的情况下检查标记是否在圆半径内
【发布时间】:2014-11-02 21:26:57
【问题描述】:

我正在使用 Google Maps 开发一个使用 Code Igniter 的应用程序,但我在检查标记的坐标是否在存储在我的数据库中的圆的半径内时遇到了一个大问题。

我会尽量用伪代码描述,对不起我是菜鸟。

  1. 右击-> 谷歌地图上出现一个带有信息气泡的圆圈,您可以在其中填写名称、颜色和圆圈半径(以米为单位);

  2. 点击保存->使用ajax发送圆心、名称、颜色等

  3. 在 AJAX 中,我将有关圆的信息存储在数据库中,但在注册之前,我需要设置在创建的圆的半径内找到的标记数量。

我已经搜索并找到了这个链接 http://www.movable-type.co.uk/scripts/latlong.html ,但我的数学不是那么好。

我需要一个函数,它接受圆、标记坐标和半径,如果标记在圆内,则返回真或假。

我做了一些我在网上找到的东西,但它不起作用

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;
}

谢谢!

【问题讨论】:

    标签: php codeigniter google-maps


    【解决方案1】:

    让我给你一些用 Javascript 计算的代码;都在谷歌地图代码中,但计算距离的函数只是一个函数,而不是一个服务。 (不知道是谁写的函数)

    您的问题是要有一个 PHP 函数,对吗?毫无疑问,您可以将 javascript 函数转换为 PHP;或者您只是信任 javascript 中的计算并使用 Ajax 发送该结果。

    代码绘制了一个圆(中心 = 布鲁塞尔;半径 = 30 公里)和 4 个标记。您可以将它们全部拖动。 点击按钮触发计算。 我通过将标记变为绿色或红色来显示结果。

    (你知道如何从这里接手吗?)

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      相关资源
      最近更新 更多