【问题标题】:Determining if Geocode falls within a Polygon/Series of Geocodes [duplicate]确定地理编码是否属于多边形/地理编码系列 [重复]
【发布时间】:2020-10-13 03:18:30
【问题描述】:

我有一系列代表销售区域的预定义区域/多边形的地理编码。例如:

-33.83585327701507, 151.2809005901216
-33.73335715102409, 150.8744770943904
-33.82163832733159, 150.8404448193081
-33.9974469167501, 151.247420749521
-33.83585327701507, 151.2809005901216

然后我有一个代表位置的单独地理编码,例如:

-33.7984533, 151.1824504

我需要确定单个位置地理编码是否在预定义的销售区域/多边形内。是否有 API 或其他方法可以以编程方式确定这一点?到目前为止,无法看到 Google Maps API。

【问题讨论】:

    标签: google-maps-api-3 maps geocode


    【解决方案1】:

    该点(-33.7984533、151.1824504)不在您提供的多边形中。要使用 Google Maps Javascript API v3 确定这一点,请使用 google.maps.geometry.poly.containsLocation 方法:

    google.maps.geometry.poly.containsLocation(new google.maps.LatLng(-33.7984533, 151.1824504), polygon)
    

    proof of concept fiddle

    代码 sn-p:

    // This example creates a simple polygon representing the Bermuda Triangle.
    
    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 5,
        center: {
          lat: 24.886,
          lng: -70.268
        },
        mapTypeId: 'terrain'
      });
    
      // Define the LatLng coordinates for the polygon's path.
      var polygonCoords = [{
          lat: -33.83585327701507,
          lng: 151.2809005901216
        },
        {
          lat: -33.73335715102409,
          lng: 150.8744770943904
        },
        {
          lat: -33.82163832733159,
          lng: 150.8404448193081
        },
        {
          lat: -33.9974469167501,
          lng: 151.247420749521
        },
        {
          lat: -33.83585327701507,
          lng: 151.2809005901216
        },
      ];
    
      // Construct the polygon.
      var polygon = new google.maps.Polygon({
        paths: polygonCoords,
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        clickable: false
      });
      polygon.setMap(map);
      var bounds = new google.maps.LatLngBounds();
      for (var i = 0; i < polygon.getPath().getLength(); i++) {
        bounds.extend(polygon.getPath().getAt(i))
      }
      map.fitBounds(bounds);
      var marker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(-33.7984533, 151.1824504)
      })
      console.log(google.maps.geometry.poly.containsLocation(new google.maps.LatLng(-33.7984533, 151.1824504), polygon));
      document.getElementById('contains').innerHTML = "containsLocation returns " + google.maps.geometry.poly.containsLocation(new google.maps.LatLng(-33.7984533, 151.1824504), polygon);
      google.maps.event.addListener(map, 'click', function(e) {
        console.log(google.maps.geometry.poly.containsLocation(e.latLng, polygon));
        document.getElementById('contains').innerHTML = "containsLocation returns " + google.maps.geometry.poly.containsLocation(e.latLng, polygon);
      });
    }
    /* Always set the map height explicitly to define the size of the div
     * element that contains the map. */
    
    #map {
      height: 80%;
    }
    
    
    /* Optional: Makes the sample page fill the window. */
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <div id="contains"></div>
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=geometry">
    </script>

    【讨论】:

    • 感谢您提供的信息 - 非常有帮助。您是否知道是否有等效的 API/方法可以在无需实际绘制地图的情况下获得结果?例如,我希望能够传入表示多边形区域的地理编码的 JSON 数组,并传入地理编码以检查并获得真/假结果。我没有明确需要映射结果,只需确定地理编码是否在多边形区域内/外。
    • 不需要映射结果,我发现当我看到它时更容易理解。但是您确实需要使用 Google Maps Javascript API v3 或找到等效的 GIS 算法来确定该点是否在多边形中。
    • 在 gis.stackexchange.com 上的搜索可能会有所帮助:google.com/…
    猜你喜欢
    • 2014-10-06
    • 2016-02-13
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多