getBounds()。包含返回false

时间:2018-05-14 11:50:06

标签: javascript google-maps

我想要检测标记并显示分配给矩形的值。通过研究,我找到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

2 个答案:

答案 0 :(得分:0)

看起来LatLngLiteralBoundsgoogle.maps.Rectangle输入已损坏。它创建了一个南北颠倒的矩形。使用原始矩形(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

screenshot of resulting map

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

有人应该在issue tracker

中创建问题

代码段

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>

答案 1 :(得分:0)

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

public static final String ACTION_CHARGING_REMINDER = "charging-reminder";
public static void executeTask(Context context, String action) {
    if (ACTION_INCREMENT_WATER_COUNT.equals(action)) {
        incrementWaterCount(context);
    } else if (ACTION_DISMISS_NOTIFICATION.equals(action)) {
        NotificationUtils.clearAllNotifications(context);
    } else if(ACTION_CHARGING_REMINDER.equals(action)){
        issueChargeReminder(context);
    }
}

随着南北方向的切换,js-fiddle返回true。