Google maps LatLngBounds-如何获取SW和NE点

Google maps LatLngBounds-如何获取SW和NE点,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我正在尝试使用谷歌地图的自动完成和偏差结果来LatLngBounds。我希望能够围绕一个岛屿(比如夏威夷群岛)画一个矩形,并以此方式获得LatLngBounds。有什么工具可以帮我吗?很久以前,我写了一个“缩放窗口”,适合你使用。它只是映射和矩形侦听器 要查看代码: 要使用它,首先手动放大并平移到岛上。要选择的整个区域必须在屏幕上可见 然后单击一次以定义矩形角。将鼠标移向对角。(将出现一个矩形选择)。再次单击以定义矩形区域。地图上方的文本区域应为您提供所选的LatLngBounds,以及SW

我正在尝试使用谷歌地图的自动完成和偏差结果来
LatLngBounds
。我希望能够围绕一个岛屿(比如夏威夷群岛)画一个矩形,并以此方式获得LatLngBounds。有什么工具可以帮我吗?

很久以前,我写了一个“缩放窗口”,适合你使用。它只是映射和矩形侦听器

要查看代码:

要使用它,首先手动放大并平移到岛上。要选择的整个区域必须在屏幕上可见

然后单击一次以定义矩形角。将鼠标移向对角。(将出现一个矩形选择)。再次单击以定义矩形区域。地图上方的文本区域应为您提供所选的LatLngBounds,以及SW和NE点

当试图包含国际日期行时,它的行为与预期不符

  var map;

  var mapOptions = {
    center: new google.maps.LatLng(0.0, 0.0),
    zoom: 2,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var bounds;
  var pt1, pt2;
  var rect;

  function toLatLng(lat, lng) {
    return new google.maps.LatLng(lat, lng);
  }

  function toBounds(j,k) {
    var pts = [];
    var latMin, latMax, lngMin, lngMax;
    var sw, ne;

    latMin = Math.min(j.lat(), k.lat());
    latMax = Math.max(j.lat(), k.lat());

    lngMin = Math.min(j.lng(), k.lng());
    lngMax = Math.max(j.lng(), k.lng());

    sw = toLatLng(latMin, lngMin);
    ne = toLatLng(latMax, lngMax);
    return new google.maps.LatLngBounds(sw, ne);
  }

  function initialize() {
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    rect = new google.maps.Rectangle({
      fillColor: "#FFFF00",
      fillOpacity: 0.3,
      strokeColor: "#0000FF",
      strokeWeight: 2
    });

    // Activate the box
    google.maps.event.addListener(map, 'click', function(event) {
      pt1 = event.latLng;
      rect.setMap(map);
    });

    // Modify the box's size
    google.maps.event.addListener(map, 'mousemove', function(event) {
      if(rect.getMap() == map) {
        rect.setBounds(toBounds(pt1, event.latLng));
      }
    });

    // Remove the zoom window and zoom in
    google.maps.event.addListener(rect, 'click', function(event) {
      rect.setMap(null);
      pt2 = event.latLng;
      myBounds = toBounds(pt1, pt2);

      document.getElementById("selectedBounds").value = "new google.maps.LatLngBounds(new google.maps.LatLng" + myBounds.getSouthWest() + ", new google.maps.LatLng" + myBounds.getNorthEast() + ");";
    });

    // Allows shrinking the box
    google.maps.event.addListener(rect, 'mousemove', function(event) {
      if(rect.getMap() == map) {
        rect.setBounds(toBounds(pt1, event.latLng));
      }
    });
  }
HTML


这是另一个与反子午线没有问题的类似工具:


马塞洛。

真棒的回答;我希望我能给这个例子加分。也许你也可以把代码贴在这里,这样以后就不会丢失了。这个工具正是我所希望的。你应该把它放在一个网站上,这样其他人可以使用它。谢谢@StackOverlownEWBIE我确实有一个计划,为我的网站清理和打包一堆地图工具,但是StackOverlown一直让我分心。这个链接现在无效。
<textarea id="selectedBounds" cols="60" rows="4"></textarea>
<div id="map_canvas"></div>​