【问题标题】:Javascript google maps contains point in rectangleJavascript 谷歌地图包含矩形中的点
【发布时间】:2018-01-13 14:23:28
【问题描述】:

我尝试检查给定的 poiont 是否在 Rectangle 内部或外部,如果在内部则返回 true,或者如果在外部则返回 false,并且我想将输出写入 textBox。 我有我的代码 ma​​p.html

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }



      #floating-panel {
        position: absolute;
        top: 4px;
        left: 9%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }

    </style>
  </head>
  <body>


  <div id="floating-panel">
      <input id="address" type="textbox" value="enter address">
      <input id="submit" type="button" value="Geocode">
      <input id="output" type="textbox" value="output">
    </div>



    <div id="map"></div>

    <script>
      var map;
      //-----This example adds a user-editable rectangle to the map.
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 45.065140, lng: 19.946804},
          zoom: 12
        });

        /*------for geocoding(I give addres not lng lang)*/
        var geocoder = new google.maps.Geocoder();

       document.getElementById('submit').addEventListener('click', function() {
          geocodeAddress(geocoder, map);
        });
        /* -------------  */

        var bounds = {
          north: 45.095140,
          south: 45.025140,
          east: 19.966804,
          west: 19.926804
        };
        // Define a rectangle and set its editable property to true.
        var rectangle = new google.maps.Rectangle({
          bounds: bounds,
          editable: true,
          draggable: true,
          geodesic: true
        });
        rectangle.setMap(map);

          document.getElementById("output").value = check_is_in_or_out(marker);


      }


      function check_is_in_or_out(marker){
      google.maps.event.addListener(map, 'bounds_changed', function() {
         var bounds = map.getBounds().contains(marker.getPosition());
      });
  return bounds;
}
      /* -------------  */
       /*------for geocoding(I give addres not lng lang)*/
       var address;
       var latitude;
       var longitude;
       var marker;
      function geocodeAddress(geocoder, resultsMap) {
         address = document.getElementById('address').value;
        geocoder.geocode({'address': address}, function(results, status) {
          if (status === 'OK') {
            resultsMap.setCenter(results[0].geometry.location);
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();
            marker = new google.maps.Marker({
              map: resultsMap,
              position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        return marker;
        });


      }
      /* -------------  */
    </script>
    <script src="http://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"
    async defer></script>
  </body>
</html>

如果给定点在内部,我想进入我的输出true,如果在外部,我想得到false 那么,我该怎么做呢?不,当我使用时没有发生任何事情:document.getElementById("output").value = check_is_in_or_out(marker); *API_KEY 如果您没有 API KEY,请改用:http://maps.google.com/maps/api/js?sensor=false

编辑 感谢 geocodezip 的帮助。现在我的代码是:

    <!DOCTYPE html>
<html>
  <head>
    <title>Map2</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }



      #floating-panel {
        position: absolute;
        top: 4px;
        left: 9%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }

    </style>
  </head>
  <body>


  <div id="floating-panel">
      <input id="address" type="textbox" value="Dobrodol">
      <input id="submit" type="button" value="Geocode">
      <input id="output" type="textbox" value="output">
    </div>



    <div id="map"></div>

    <script>
   function check_is_in_or_out(marker) {
  var insideRectangle = false;
  if (rectangle && rectangle.getBounds && marker && marker.getPosition())
    insideRectangle = rectangle.getBounds().contains(marker.getPosition());

  return insideRectangle;
}

var map;
var rectangle;
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: 45.065140,
      lng: 19.946804
    },
    zoom: 12
  });

  var geocoder = new google.maps.Geocoder();

  document.getElementById('submit').addEventListener('click', function() {
    geocodeAddress(geocoder, map);
  });
  var bounds = {
    north: 45.095140,
    south: 45.025140,
    east: 19.966804,
    west: 19.926804
  };
  // Define a rectangle and set its editable property to true.
  rectangle = new google.maps.Rectangle({
    bounds: bounds,
    editable: true,
    draggable: true,
    geodesic: true
  });
  rectangle.setMap(map);


}
var address;
var latitude;
var longitude;
var marker;

function geocodeAddress(geocoder, resultsMap) {
  address = document.getElementById('address').value;
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status === 'OK') {
      resultsMap.setCenter(results[0].geometry.location);
      latitude = results[0].geometry.location.lat();
      longitude = results[0].geometry.location.lng();
      marker = new google.maps.Marker({
        map: resultsMap,
        position: results[0].geometry.location
      });
      document.getElementById("output").value = check_is_in_or_out(marker);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
    </script>
    <script src="http://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"
    async defer></script>
  </body>
</html>

geocodeAddress(geocoder, resultsMap) 中很好,我们使用document.getElementById("output").value = check_is_in_or_out(marker); 并且我的文本框更改为真或假。但是我怎样才能将这个值设置为 var?像这样的东西: var bool = check_is_in_or_out(marker);在 bool 中我会有真假? 我尝试在 geocodeAddress 中使用 return 但我的变量未定义,所以在我看来我只能在内部函数 geocoder.geocode 中执行此操作,但我希望这个变量是全局的而不是本地的。

【问题讨论】:

    标签: javascript html google-maps


    【解决方案1】:

    更改您的check_is_in_or_out 以使用可编辑矩形作为边界contains 检查:

    function check_is_in_or_out(marker){
      var insideRectangle = false;
      if (rectangle && rectangle.getBounds && marker && marker.getPosition()) 
        insideRectangle = rectangle.getBounds().contains(marker.getPosition());
    
      return insideRectangle;
    }
    

    每当标记位置或 矩形 边界发生变化时调用它(如果您想检查它是否在可编辑矩形内,则地图边界不相关)。

    proof of concept fiddle

    代码 sn-p:

    function check_is_in_or_out(marker) {
      var insideRectangle = false;
      if (rectangle && rectangle.getBounds && marker && marker.getPosition())
        insideRectangle = rectangle.getBounds().contains(marker.getPosition());
    
      return insideRectangle;
    }
    
    var map;
    var rectangle;
    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        center: {
          lat: 45.065140,
          lng: 19.946804
        },
        zoom: 12
      });
    
      var geocoder = new google.maps.Geocoder();
    
      document.getElementById('submit').addEventListener('click', function() {
        geocodeAddress(geocoder, map);
      });
      var bounds = {
        north: 45.095140,
        south: 45.025140,
        east: 19.966804,
        west: 19.926804
      };
      // Define a rectangle and set its editable property to true.
      rectangle = new google.maps.Rectangle({
        bounds: bounds,
        editable: true,
        draggable: true,
        geodesic: true
      });
      rectangle.setMap(map);
    
      document.getElementById("output").value = check_is_in_or_out(marker);
    
      google.maps.event.addListener(rectangle, 'bounds_changed', function() {
        document.getElementById("output").value = check_is_in_or_out(marker);
      });
    }
    var address;
    var latitude;
    var longitude;
    var marker;
    
    function geocodeAddress(geocoder, resultsMap) {
      address = document.getElementById('address').value;
      geocoder.geocode({
        'address': address
      }, function(results, status) {
        if (status === 'OK') {
          resultsMap.setCenter(results[0].geometry.location);
          latitude = results[0].geometry.location.lat();
          longitude = results[0].geometry.location.lng();
          marker = new google.maps.Marker({
            map: resultsMap,
            position: results[0].geometry.location
          });
          document.getElementById("output").value = check_is_in_or_out(marker);
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
    }
    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <div id="floating-panel">
      <input id="address" type="textbox" value="Dobrodol">
      <input id="submit" type="button" value="Geocode">
      <input id="output" type="textbox" value="output">
    </div>
    <div id="map"></div>
    
    <script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      相关资源
      最近更新 更多