【问题标题】:getBounds().contains returning falsegetBounds().contains 返回 false
【发布时间】:2018-05-14 11:50:05
【问题描述】:

我希望检测到标记并显示分配给矩形的值。通过研究,我找到了this,但它不适用于我的情况,因为我只需要一个 latlong 点。我找到了与我想要的类似的东西并尝试了一下,即here

但它似乎不适用于我的情况。 我在下面有一个矩形:

var rectangle1 = new google.maps.Rectangle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
bounds: {
  north: -37.822802,
  south: -37.822260,
  east: 145.036010,
  west: 145.035324
}
});

我尝试插入一个点,即-37.822545, 145.035526,这个点在矩形内,它应该返回true,但事实并非如此。 Here is my JSfiddle

【问题讨论】:

  • 在我看来那个点在矩形 (fiddle) 中,对我来说似乎是一个错误。
  • @Craicerjack 但在document 中它说“如果给定的纬度/经度在这个范围内,则返回真。”,它应该检测矩形内的任何纬度点,到什么我明白了。
  • @geocodezip 我希望不是这样,因为我需要弄清楚这一点:(

标签: javascript google-maps


【解决方案1】:

看起来google.maps.RectangleLatLngLiteralBounds 输入已损坏。它创建了一个南北颠倒的矩形。使用您原来的矩形 (north: -37.822802, south: -37.822260),我得到:

NE1:-37.822802,145.03601  (north=-37.822802, incorrect)
SW1:-37.82226,145.035324  (south=-37.82226, incorrect) 

如果我用google.maps.LatLngBounds 对象创建矩形,我会得到:

NE2:-37.82226,145.03601   (north=-37.82226, correct)
SW2:-37.822802,145.035324 (south=-37.822802, correct)

proof of concept fiddle

(蓝色标记为rectangle2的NE/SW,红色标记为rectangle1的NE/SW。)

有人应该在issue tracker 中创建问题

代码 sn-p:

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 19,
    center: {
      lat: -37.82280243352756,
      lng: 145.0353240966797
    },
    mapTypeId: 'terrain'
  });
  var rectangle1 = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: {
      north: -37.822802,
      south: -37.822260,
      east: 145.036010,
      west: 145.035324
    }
  });
  var rectangle2 = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: new google.maps.LatLngBounds(
      new google.maps.LatLng(-37.822802, 145.035324), // SW
      new google.maps.LatLng(-37.822260, 145.036010)) // NE
  });
  var marker = new google.maps.Marker({
    map: map,
    position: {
      lat: -37.822545,
      lng: 145.035526
    },
    title: "-37.822545, 145.035526"

  });
  var NEmark1 = new google.maps.Marker({
    map: map,
    position: rectangle1.getBounds().getNorthEast(),
    title: "NE1:" + rectangle1.getBounds().getNorthEast().toUrlValue()
  });
  var SWmark1 = new google.maps.Marker({
    map: map,
    position: rectangle1.getBounds().getSouthWest(),
    title: "SW1:" + rectangle1.getBounds().getSouthWest().toUrlValue()
  });
  var NEmark2 = new google.maps.Marker({
    map: map,
    position: rectangle2.getBounds().getNorthEast(),
    title: "NE2:" + rectangle2.getBounds().getNorthEast().toUrlValue(),
    icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
  });
  var SWmark2 = new google.maps.Marker({
    map: map,
    position: rectangle2.getBounds().getSouthWest(),
    title: "SW2:" + rectangle2.getBounds().getSouthWest().toUrlValue(),
    icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
  });
  map.fitBounds(rectangle1.getBounds());
  console.log(rectangle1.getBounds().toUrlValue());

  console.log("NE1:" + rectangle1.getBounds().getNorthEast().toUrlValue());
  console.log("SW1:" + rectangle1.getBounds().getSouthWest().toUrlValue());
  document.getElementById('contains').innerHTML = "rectangle1 bounds.contains=" + rectangle1.getBounds().contains({
    lat: -37.822545,
    lng: 145.035526
  });
  console.log(marker.getPosition().toUrlValue());
  console.log(rectangle2.getBounds().toUrlValue());
  console.log("NE2:" + rectangle2.getBounds().getNorthEast().toUrlValue());
  console.log("SW2:" + rectangle2.getBounds().getSouthWest().toUrlValue());
  console.log(rectangle2.getBounds().contains({
    lat: -37.822545,
    lng: 145.035526
  }));
  document.getElementById('contains').innerHTML += " rectangle2 bounds.contains=" + rectangle2.getBounds().contains({
    lat: -37.822545,
    lng: 145.035526
  });
  console.log(rectangle2.getBounds().contains(marker.getPosition()));
}
html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

#map {
  height: 95%;
  width: 100%;
}
<div id="contains"></div>
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

【讨论】:

  • 感谢详细的解释,这真的很有帮助
【解决方案2】:

我认为您只是混淆了南北参数。 (较大的负纬度数比较小的负纬度数更南)

function initMap() {


var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 19,
    center: {
      lat: -37.82280243352756,
      lng: 145.0353240966797
    },
    mapTypeId: 'terrain'
  });

  var rectangle1 = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: {
      south: -37.822802,  // This is further south ...
      north: -37.822260,  // than this
      east: 145.036010,
      west: 145.035324
    }
  });
  alert(rectangle1.getBounds());
  alert(rectangle1.getBounds().contains({lat:-37.822545, lng:145.035526}));
}

像这样切换南北,js-fiddle返回true。

【讨论】:

  • 我认为它仍然可以创建相同的形状,它应该可以工作,但是,是的,这也是一种方法。谢谢
  • 我理解您的思路,但从技术上讲,从例如南:-37 到北:-38 的纬度范围,将在两极和地球的另一侧环绕 359 度。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 2021-01-28
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
  • 2020-09-15
相关资源
最近更新 更多