• <bdo id='8ksa3'></bdo><ul id='8ksa3'></ul>

    <small id='8ksa3'></small><noframes id='8ksa3'>

      <tfoot id='8ksa3'></tfoot>

    1. <legend id='8ksa3'><style id='8ksa3'><dir id='8ksa3'><q id='8ksa3'></q></dir></style></legend>
      1. <i id='8ksa3'><tr id='8ksa3'><dt id='8ksa3'><q id='8ksa3'><span id='8ksa3'><b id='8ksa3'><form id='8ksa3'><ins id='8ksa3'></ins><ul id='8ksa3'></ul><sub id='8ksa3'></sub></form><legend id='8ksa3'></legend><bdo id='8ksa3'><pre id='8ksa3'><center id='8ksa3'></center></pre></bdo></b><th id='8ksa3'></th></span></q></dt></tr></i><div id='8ksa3'><tfoot id='8ksa3'></tfoot><dl id='8ksa3'><fieldset id='8ksa3'></fieldset></dl></div>

        Google maps JS API v3:使用 containsLocation() 获取圆圈中

        时间:2023-09-08
        <i id='nLZ0B'><tr id='nLZ0B'><dt id='nLZ0B'><q id='nLZ0B'><span id='nLZ0B'><b id='nLZ0B'><form id='nLZ0B'><ins id='nLZ0B'></ins><ul id='nLZ0B'></ul><sub id='nLZ0B'></sub></form><legend id='nLZ0B'></legend><bdo id='nLZ0B'><pre id='nLZ0B'><center id='nLZ0B'></center></pre></bdo></b><th id='nLZ0B'></th></span></q></dt></tr></i><div id='nLZ0B'><tfoot id='nLZ0B'></tfoot><dl id='nLZ0B'><fieldset id='nLZ0B'></fieldset></dl></div>
        <legend id='nLZ0B'><style id='nLZ0B'><dir id='nLZ0B'><q id='nLZ0B'></q></dir></style></legend>

        1. <tfoot id='nLZ0B'></tfoot>

                <bdo id='nLZ0B'></bdo><ul id='nLZ0B'></ul>

                • <small id='nLZ0B'></small><noframes id='nLZ0B'>

                    <tbody id='nLZ0B'></tbody>
                  本文介绍了Google maps JS API v3:使用 containsLocation() 获取圆圈中的标记不起作用 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试按照推荐使用 google.maps.geometry.poly.containsLocation 来获取给定半径 (google.maps.Circle) 内的所有标记 这里,但我明白了一个错误:TypeError: e is undefined.

                  I'm trying to get all markers within a given radius (google.maps.Circle) by using google.maps.geometry.poly.containsLocation as recommended here, but I get an error: TypeError: e is undefined.

                  片段:

                  // ...
                  if (google.maps.geometry.poly.containsLocation(randomMarkers[i].marker.getPosition(), searchArea)) {
                      console.log('=> is in searchArea');
                  } else {
                      console.log('=> is NOT in searchArea');
                  }
                  // ...
                  

                  完整代码:

                  <!DOCTYPE html>
                  <html>
                  <head>
                  <title>Simple Map</title>
                  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
                  <meta charset="utf-8">
                  <style>
                    html, body, #map-canvas {
                      height: 100%;
                      margin: 0px;
                      padding: 0px
                    }
                  </style>
                  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
                  <script>
                  var map,
                      searchArea,
                      searchAreaMarker,
                      searchAreaRadius = 1000, // metres
                      startLat = 40.782827,
                      startLng = -73.966167       
                  ;
                  
                  function init() {   
                      var startLatLng = new google.maps.LatLng(startLat, startLng);
                  
                      map = new google.maps.Map(document.getElementById('map-canvas'), {
                          center: startLatLng,
                          zoom: 12
                      });
                  
                      searchArea = new google.maps.Circle({
                          strokeColor: '#FF0000',
                          strokeOpacity: 0.5,
                          strokeWeight: 2,
                          fillColor: '#FF0000',
                          fillOpacity: 0.2,
                          map: map,
                          center: startLatLng,
                          radius: searchAreaRadius
                      });
                  
                      searchAreaMarker = new google.maps.Marker({
                          position: startLatLng,
                          map: map,
                          draggable: true,
                          animation: google.maps.Animation.DROP,
                          title: 'searchAreaMarker'
                      });
                  
                      var randomMarkers = [
                          { title: 'Marker 1', latLng: new google.maps.LatLng(40.770088, -73.971146) },
                          { title: 'Marker 2', latLng: new google.maps.LatLng(40.782048, -73.972691) },
                          { title: 'Marker 3', latLng: new google.maps.LatLng(40.769048, -73.987797) },
                          { title: 'Marker 4', latLng: new google.maps.LatLng(40.773858, -73.956211) },
                          { title: 'Marker 5', latLng: new google.maps.LatLng(40.800372, -73.952091) },
                          { title: 'Marker 6', latLng: new google.maps.LatLng(40.804661, -73.939388) }            
                      ];
                  
                      for (var i = 0; i < randomMarkers.length; i++) {
                          randomMarkers[i].marker = new google.maps.Marker({
                              position: randomMarkers[i].latLng,
                              map: map,
                              title: randomMarkers[i].title
                          });
                      }
                  
                      google.maps.event.addListener(searchAreaMarker, 'dragend', function(e) {
                          startLatLng = e.latLng;
                  
                          searchArea.setOptions({
                              center: startLatLng
                          });
                  
                          map.panTo(searchAreaMarker.getPosition());
                  
                          // find markers in area
                          for (var i = 0; i < randomMarkers.length; i++) {
                              console.log('Marker: ' + randomMarkers[i].marker.title + ', position: ' + randomMarkers[i].marker.getPosition()); 
                  
                              // ---------- Here comes the error: 
                              // TypeError: e is undefined
                              if (google.maps.geometry.poly.containsLocation(randomMarkers[i].marker.getPosition(), searchArea)) {
                                  console.log('=> is in searchArea');
                              } else {
                                  console.log('=> is NOT in searchArea');
                              }
                          }
                      });
                  }
                  
                  google.maps.event.addDomListener(window, 'load', init);
                  </script>
                  


                  推荐答案

                  containsLocation 是 google.maps.Polygon 上的一种方法对象 不是 google.maps.Circle 对象

                  要确定标记是否在圆圈内,请使用 google.maps.geometry.球形.computeDistanceBetween

                  To determine if a marker is within a circle use google.maps.geometry.spherical.computeDistanceBetween

                  if (google.maps.geometry.spherical.computeDistanceBetween(randomMarkers[i].marker.getPosition(), searchArea.getCenter()) <= searchArea.getRadius()) {
                      console.log('=> is in searchArea');
                  } else {
                      console.log('=> is NOT in searchArea');
                  }
                  

                  工作小提琴

                  工作代码片段:

                  var map,
                    searchArea,
                    searchAreaMarker,
                    searchAreaRadius = 1000, // metres
                    startLat = 40.782827,
                    startLng = -73.966167;
                  
                  function init() {
                    var startLatLng = new google.maps.LatLng(startLat, startLng);
                  
                    map = new google.maps.Map(document.getElementById('map-canvas'), {
                      center: startLatLng,
                      zoom: 12
                    });
                  
                    searchArea = new google.maps.Circle({
                      strokeColor: '#FF0000',
                      strokeOpacity: 0.5,
                      strokeWeight: 2,
                      fillColor: '#FF0000',
                      fillOpacity: 0.2,
                      map: map,
                      center: startLatLng,
                      radius: searchAreaRadius
                    });
                  
                    searchAreaMarker = new google.maps.Marker({
                      position: startLatLng,
                      map: map,
                      draggable: true,
                      animation: google.maps.Animation.DROP,
                      title: 'searchAreaMarker'
                    });
                  
                    var randomMarkers = [{
                      title: 'Marker 1',
                      latLng: new google.maps.LatLng(40.770088, -73.971146)
                    }, {
                      title: 'Marker 2',
                      latLng: new google.maps.LatLng(40.782048, -73.972691)
                    }, {
                      title: 'Marker 3',
                      latLng: new google.maps.LatLng(40.769048, -73.987797)
                    }, {
                      title: 'Marker 4',
                      latLng: new google.maps.LatLng(40.773858, -73.956211)
                    }, {
                      title: 'Marker 5',
                      latLng: new google.maps.LatLng(40.800372, -73.952091)
                    }, {
                      title: 'Marker 6',
                      latLng: new google.maps.LatLng(40.804661, -73.939388)
                    }];
                  
                    for (var i = 0; i < randomMarkers.length; i++) {
                      randomMarkers[i].marker = new google.maps.Marker({
                        position: randomMarkers[i].latLng,
                        map: map,
                        title: randomMarkers[i].title
                      });
                    }
                  
                    google.maps.event.addListener(searchAreaMarker, 'dragend', function(e) {
                      startLatLng = e.latLng;
                  
                      searchArea.setOptions({
                        center: startLatLng
                      });
                  
                      map.panTo(searchAreaMarker.getPosition());
                      findMarkersInArea();
                    });
                    var iwArray = [];
                    function findMarkersInArea() {
                      // close open infowindows
                      for (var i=0; i<iwArray.length; i++) {
                        iwArray[i].close();
                      }
                      iwArray = [];
                      // find markers in area
                      for (var i = 0; i < randomMarkers.length; i++) {
                        console.log('Marker: ' + randomMarkers[i].marker.title + ', position: ' + randomMarkers[i].marker.getPosition());
                        console.log("marker["+i+"] posn="+randomMarkers[i].marker.getPosition().toUrlValue(6));
                        if (google.maps.geometry.spherical.computeDistanceBetween(randomMarkers[i].marker.getPosition(), searchArea.getCenter()) <= searchArea.getRadius()) {
                          console.log('=> is in searchArea');
                          var iw = new google.maps.InfoWindow();
                          iw.setContent("is in searchArea");
                          iw.open(map, randomMarkers[i].marker);
                          iwArray.push(iw);
                        } else {
                          console.log('=> is NOT in searchArea');
                          var iw = new google.maps.InfoWindow();
                          iw.setContent("outside searchArea");
                          iw.open(map, randomMarkers[i].marker);
                          iwArray.push(iw);
                        }
                      }
                    }
                    // initial config
                    findMarkersInArea();
                  }
                  
                  google.maps.event.addDomListener(window, 'load', init);

                  html,
                  body,
                  #map-canvas {
                    height: 100%;
                    width: 100%;
                    margin: 0px;
                    padding: 0px
                  }

                  <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
                  <div id="map-canvas" style="border: 2px solid #3872ac;"></div>

                  这篇关于Google maps JS API v3:使用 containsLocation() 获取圆圈中的标记不起作用 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么 Math.cos(90 * Math.PI/180) 产生 6.123031769111... 下一篇:clip-path:circle() 半径似乎没有正确计算

                  相关文章

                  最新文章

                • <legend id='SPZvJ'><style id='SPZvJ'><dir id='SPZvJ'><q id='SPZvJ'></q></dir></style></legend>
                  <i id='SPZvJ'><tr id='SPZvJ'><dt id='SPZvJ'><q id='SPZvJ'><span id='SPZvJ'><b id='SPZvJ'><form id='SPZvJ'><ins id='SPZvJ'></ins><ul id='SPZvJ'></ul><sub id='SPZvJ'></sub></form><legend id='SPZvJ'></legend><bdo id='SPZvJ'><pre id='SPZvJ'><center id='SPZvJ'></center></pre></bdo></b><th id='SPZvJ'></th></span></q></dt></tr></i><div id='SPZvJ'><tfoot id='SPZvJ'></tfoot><dl id='SPZvJ'><fieldset id='SPZvJ'></fieldset></dl></div>
                  <tfoot id='SPZvJ'></tfoot>
                    <bdo id='SPZvJ'></bdo><ul id='SPZvJ'></ul>

                  <small id='SPZvJ'></small><noframes id='SPZvJ'>