【问题标题】:Search Map Coordinates in Multiple Boundaries在多个边界中搜索地图坐标
【发布时间】:2014-02-14 03:50:20
【问题描述】:

我有一些代码向用户询问地址并使用 GoogleMaps API 返回该地址的地理编码纬度/经度。我已经设置它来查询这些坐标是否在预定的边界内。我想做的是有多个边界,并且能够搜索它们并返回结果。例如,我会有一些 JSON 数据或一些其他类型的数组,其中包含 SW_LatLng、NE_LatLng 和一个答案。所以我的查询将使用以下函数,如果为真则返回答案。我的问题是如何遍历多个边界并返回结果。

bounds.contains(new google.maps.LatLng(latitude,longitude));

这是我已有的代码:

function codeAddress() {
    var bounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(37,-109),
        new google.maps.LatLng(41, -102)
    );
    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById('address').value;

    geocoder.geocode( { 'address': address}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK) {
        var latitude = results[0].geometry.location.lat();
        var longitude = results[0].geometry.location.lng();

      }
      var center = bounds.getCenter();
      var x = bounds.contains(new google.maps.LatLng(latitude,longitude));

      if (x) { alert('In Colorado')} else { alert('Outside Colorado')};

    });
  }

<h3>Is your Address in Colorado?</h3>
<input id="address" type="textbox" style="width:60%">
<input type="button" value="Find" onclick="codeAddress()">

我在 CodePen(如 JSFiddle)中设置了这个供您使用。

CodePen Link

感谢任何帮助。另外,隐藏数据的最佳方法是什么?

这里是 JSFiddle 链接JSFiddle Link

【问题讨论】:

  • 您在 fiddle 和 codepen 中的代码已损坏:Uncaught ReferenceError: initialize is not defined
  • 我修复了 JSFiddle,现在它可以工作了。 CodePen 对我来说很好用。

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


【解决方案1】:

您是否考虑过使用多边形来定义您的边界?然后使用几何库中的google maps api containsLocation方法(https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation)来确定该点是否在边界内?

您似乎仅限于矩形区域(这可能足以满足您的目的,我不知道我只是提出建议)。

无论哪种方式,正如你所说,你需要遍历你的纬度/经度边界对数组(称之为latLngArray)。

function codeAddress() {

    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById('address').value;

    geocoder.geocode( { 'address': address}, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();

        }

        var latLngArrayLength = latLngArray.getLength();

        for (i=0; i<latLngArrayLength; i++) {
            var bounds = new google.maps.LatLngBounds(
                new google.maps.LatLng(latLngArray[i].latSW, latLngArray[i].lngSW)
                new google.maps.LatLng(latLngArray[i].latNE, latLngArray[i].lngNE)
            );

            var center = bounds.getCenter(); //what is this for??
            var x = bounds.contains(new google.maps.LatLng(latitude,longitude));

            if (x) { 
                alert('In ' + latLngArray[i].boundaryName); //not sure what you want to return exactly
                return latLngArray[i].boundaryName;
            }
        }  
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-08
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多