google-maps - 在 Google map 中绘制方框/矩形选区

标签 google-maps google-maps-api-3

我正在开发 Google map ,希望实现一项功能,用户可以使用鼠标绘制一个方框/矩形来选择 map 上的区域(就像在窗口中选择多个文件一样)。选择后,我想获取该区域中的所有标记。我一直在查看 Google map api 和搜索,但找不到解决方案。我尝试使用 jQuery Selectable 进行选择,但它返回的只是一堆 div,我无法确定是否选择了任何标记。

最佳答案

我找到了一个 Library keydragzoom ( http://google-maps-utility-library-v3.googlecode.com/svn/tags/keydragzoom/1.0/docs/reference.html ) 并用它在页面上绘制了一个矩形。

后来,我编辑了库并阻止它缩放所选区域,而是让它在“dragend”事件中返回正确的坐标。然后,我手动循环遍历 map 上的所有标记,以查找该特定区域内的标记。图书馆没有为我提供正确的坐标,因此我进行了以下更改。

将 DragZoom 函数更改为

    var prj = null;
    function DragZoom(map, opt_zoomOpts) {
        var ov = new google.maps.OverlayView();

        var me = this;
        ov.onAdd = function () {
            me.init_(map, opt_zoomOpts);
        };
        ov.draw = function () {
        };
        ov.onRemove = function () {
        };
        ov.setMap(map);
        this.prjov_ = ov;
        google.maps.event.addListener(map, 'idle', function () {
            prj = ov.getProjection();
        });
    }

并将DragZoom.prototype.onMouseUp_函数添加到

DragZoom.prototype.onMouseUp_ = function (e) {
    this.mouseDown_ = false;
    if (this.dragging_) {
      var left = Math.min(this.startPt_.x, this.endPt_.x);
      var top = Math.min(this.startPt_.y, this.endPt_.y);
      var width = Math.abs(this.startPt_.x - this.endPt_.x);
      var height = Math.abs(this.startPt_.y - this.endPt_.y);
      var points={
        top: top,
        left: left,
        bottom: top + height,
        right: left + width
       };
      var prj = this.prjov_.getProjection();
      // 2009-05-29: since V3 does not have fromContainerPixel, 
      //needs find offset here
      var containerPos = getElementPosition(this.map_.getDiv());
      var mapPanePos = getElementPosition(this.prjov_.getPanes().mapPane);
      left = left + (containerPos.left - mapPanePos.left);
      top = top + (containerPos.top - mapPanePos.top);
      var sw = prj.fromDivPixelToLatLng(new google.maps.Point(left, top + height));
      var ne = prj.fromDivPixelToLatLng(new google.maps.Point(left + width, top));
      var bnds = new google.maps.LatLngBounds(sw, ne);
      //this.map_.fitBounds(bnds); 
      this.dragging_ = false;
      this.boxDiv_.style.display = 'none';
      /**
       * This event is fired when the drag operation ends. 
       * Note that the event is not fired if the hot key is released before the drag operation ends.
       * @name DragZoom#dragend
       * @param {GLatLngBounds} newBounds
       * @event
       */

      google.maps.event.trigger(this, 'dragend', points);
    }
  };

关于google-maps - 在 Google map 中绘制方框/矩形选区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7976196/

相关文章:

google-maps-api-3 - 如何在 OpenLayers 中禁用 google "bird' 的眼睛 View ?

google-maps - Google Maps API V3 - 通过点击事件更改圆半径

javascript - 谷歌地图 v3 directionsRenderer.setMap(null) 无法清除以前的方向

android - map 的 android.content.Intent.ACTION_VIEW 是不可用还是我错了?

google-maps - 谷歌地图 : Border on Markers

iphone - 在 iOS 中谷歌查询经度和纬度时没有得到 json 响应?

javascript - 如何从 map 上删除带有粘合圆的标记?

google-maps - 图层中的Google Maps Api标记

javascript - 自定义 Google Map API V3 缩放按钮

javascript - 在多个边界中搜索 map 坐标