如何知道谷歌地图API中的标记是否在多边形内

发布于 2024-12-13 17:32:19 字数 2877 浏览 1 评论 0原文

当您单击地图中的任意位置时,会创建 1 个新标记并创建多边形 它工作正常,但现在我的问题是我无法得到来自数据库的标记是否在多边形内部,如何知道它???

<?php include("config.php"); ?>
<?php $result = mysql_query("select * from `googlemaps`"); ?>
<html>
<head>
<title>Google Maps</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var poly, map;
  var markers = [];
  var path = new google.maps.MVCArray;

  function initialize() {
    var LatLng = new google.maps.LatLng(23.183, 75.767);

    map = new google.maps.Map(document.getElementById("map"), {
      zoom: 12,
      center: LatLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });


  function createMarker(point, title) {

       var infowindow = new google.maps.InfoWindow({
        content: "<span style='color:blue;'>"+title+"</span>"
    });   
      var marker =  new google.maps.Marker({
          position: point, 
          map: map, 
          title:title
      });

      marker.Image ='https://d3szoh0f46td6t.cloudfront.net/public/1626980/small';
     google.maps.event.addListener(marker, "click", function() { 
     infowindow.open(map,marker);
      });
        return marker;
    }
<?php
        if(mysql_num_rows($result)>0)
        {
          while( $data = mysql_fetch_array($result) )
         { ?>
         createMarker(new google.maps.LatLng(<?php echo $data['lat']; ?>, <?php echo $data['lon']; ?>),  "<?php echo $data['title']; ?>");

    <?php } 
         }
?>  
    poly = new google.maps.Polygon({
      strokeWeight: 3,
      fillColor: '#5555FF'
    });
    poly.setMap(map);
    poly.setPaths(new google.maps.MVCArray([path]));

    google.maps.event.addListener(map, 'click', addPoint);
  }

  function addPoint(event) {
    path.insertAt(path.length, event.latLng);

    var marker = new google.maps.Marker({
      position: event.latLng,
      map: map,
      draggable: true
    });

    markers.push(marker);
    marker.setTitle("#" + path.length);

    google.maps.event.addListener(marker, 'click', function() {
      marker.setMap(null);
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      markers.splice(i, 1);
      path.removeAt(i);
      }
    );

    google.maps.event.addListener(marker, 'dragend', function() {
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      path.setAt(i, marker.getPosition());
      document.getElementById("abc").innerHTML=path.setAt(i, marker.getPosition());
      }
    );
  }

</script>
</head>
<body style="margin:0px; padding:0px;" onLoad="initialize()">
  <div id="map" style="width:500px; height:500px;"></div>
    <div id="abc"></div>
</body>
</html>

When You click on anywhere in map 1 new marker created and make polygon
its working fine but now my problem is i can't get is my marker which is coming from database is inside polygon or not, how to know it???

<?php include("config.php"); ?>
<?php $result = mysql_query("select * from `googlemaps`"); ?>
<html>
<head>
<title>Google Maps</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var poly, map;
  var markers = [];
  var path = new google.maps.MVCArray;

  function initialize() {
    var LatLng = new google.maps.LatLng(23.183, 75.767);

    map = new google.maps.Map(document.getElementById("map"), {
      zoom: 12,
      center: LatLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });


  function createMarker(point, title) {

       var infowindow = new google.maps.InfoWindow({
        content: "<span style='color:blue;'>"+title+"</span>"
    });   
      var marker =  new google.maps.Marker({
          position: point, 
          map: map, 
          title:title
      });

      marker.Image ='https://d3szoh0f46td6t.cloudfront.net/public/1626980/small';
     google.maps.event.addListener(marker, "click", function() { 
     infowindow.open(map,marker);
      });
        return marker;
    }
<?php
        if(mysql_num_rows($result)>0)
        {
          while( $data = mysql_fetch_array($result) )
         { ?>
         createMarker(new google.maps.LatLng(<?php echo $data['lat']; ?>, <?php echo $data['lon']; ?>),  "<?php echo $data['title']; ?>");

    <?php } 
         }
?>  
    poly = new google.maps.Polygon({
      strokeWeight: 3,
      fillColor: '#5555FF'
    });
    poly.setMap(map);
    poly.setPaths(new google.maps.MVCArray([path]));

    google.maps.event.addListener(map, 'click', addPoint);
  }

  function addPoint(event) {
    path.insertAt(path.length, event.latLng);

    var marker = new google.maps.Marker({
      position: event.latLng,
      map: map,
      draggable: true
    });

    markers.push(marker);
    marker.setTitle("#" + path.length);

    google.maps.event.addListener(marker, 'click', function() {
      marker.setMap(null);
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      markers.splice(i, 1);
      path.removeAt(i);
      }
    );

    google.maps.event.addListener(marker, 'dragend', function() {
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      path.setAt(i, marker.getPosition());
      document.getElementById("abc").innerHTML=path.setAt(i, marker.getPosition());
      }
    );
  }

</script>
</head>
<body style="margin:0px; padding:0px;" onLoad="initialize()">
  <div id="map" style="width:500px; height:500px;"></div>
    <div id="abc"></div>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-12-20 17:32:19

您可能会问如何检查此 javascript 或 php。我不知道这些领域有 GIS 框架。

大多数空间数据库(例如我使用过的 SQL Server/Postgresql)都提供了诸如 ST_Contains 或 ST_Distance 之类的功能,您可以使用它们。我认为这是 OpenGIS 规范的一部分。

我想你可能正在使用 MYSQL。所以可能只需要搜索一个函数。

You might be asking how to check this javascript or php. I am not aware of a GIS framework in these areas.

Most of the spatial databases (like SQL Server/Postgresql which I have worked with) provide features like ST_Contains or ST_Distance which you can make use of. I think this is part of OpenGIS specification.

I think you might probably be using MYSQL. So may just need to search for a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文