javascript - 谷歌地图拖动矩形来选择标记

标签 javascript jquery google-maps-api-3

我正在尝试在谷歌地图上绘制一个矩形并检测是否有任何标记位于该矩形的边界内。

要绘制矩形,请按住 Shift 键,单击并拖动。

我这里有一个工作样本 - http://jsfiddle.net/dbwPQ/3/

为什么仅当矩形是从左下角到右上角或从右上角到左下角绘制时,.contains 方法才返回 true。

但是从左上角到右下角或右下角到左上角绘制的相同区域返回 false????

                if (boundsSelectionArea.contains(markers[key].position))
            //if (gribBoundingBox.getBounds().getNorthEast().lat() <= markers[key].position.lat() &&
            //    gribBoundingBox.getBounds().getSouthWest().lat() >= markers[key].position.lat() && 
            //    gribBoundingBox.getBounds().getSouthWest().lng() <= markers[key].position.lng() && 
            //    gribBoundingBox.getBounds().getNorthEast().lng() >= markers[key].position.lng() ) 
            {
                //if(flashMovie !== null && flashMovie !== undefined) {
                console.log("User selected:" + key + ", id:" + markers[key].id);
                //}
            } else {
                //if(flashMovie !== null && flashMovie !== undefined) {
                console.log("User NOT selected:" + key + ", id:" + markers[key].id);
                //} 
            }

UPDATE 这有效,但我不知道为什么? http://jsfiddle.net/dbwPQ/4/

最佳答案

看起来 google.maps.Rectangle.getBounds 正在返回“无效”边界

key:Vince posn:(53.801279, -1.5485670000000482) out of bnds:((55.45394132943307, -4.0869140625), (52.72298552457069, 1.7138671875))
key:Vince posn:(53.801279, -1.5485670000000482) in bnds:((52.26815737376817, -4.04296875), (55.65279803318956, 2.2412109375))

如果您用两个 Angular 扩展一个空的 google.maps.LatLng 对象,它将起作用:

google.maps.event.addListener(themap, 'mousemove', function (e) {
    if (mouseIsDown && shiftPressed) {
        if (gribBoundingBox !== null) // box exists
        {
            bounds.extend(e.latLng);                
            gribBoundingBox.setBounds(bounds); // If this statement is enabled, I lose mouseUp events

        } else // create bounding box
        {
            bounds = new google.maps.LatLngBounds();
            bounds.extend(e.latLng);
            gribBoundingBox = new google.maps.Rectangle({
                map: themap,
                bounds: bounds,
                fillOpacity: 0.15,
                strokeWeight: 0.9,
                clickable: false
            });
        }
    }
});

working example

代码片段:

var map;
var markers = {};
var bounds = null;
// add marker to object
var posi = new google.maps.LatLng(53.801279, -1.548567);
var name = 'Vince';
markers[name] = {};
markers[name].id = 1;
markers[name].lat = 53.801279;
markers[name].lng = -1.548567;
markers[name].state = 'Online';
markers[name].position = posi;
markers[name].selected = false;

$(document).ready(function() {

  var mapOptions = {
    zoom: 5,
    center: new google.maps.LatLng(53.801279, -1.548567),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
  var infowindow = new google.maps.InfoWindow();
  for (var key in markers) {
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(markers[key].lat, markers[key].lng),
      map: map
    });
    markers[key].marker = marker;

    google.maps.event.addListener(marker, 'click', (function(marker, key) {
      return function() {
        infowindow.setContent(key);
        infowindow.open(map, marker);
      }
    })(marker, key));
  }

  // Start drag rectangle to select markers !!!!!!!!!!!!!!!!
  var shiftPressed = false;

  $(window).keydown(function(evt) {
    if (evt.which === 16) { // shift
      shiftPressed = true;
    }
  }).keyup(function(evt) {
    if (evt.which === 16) { // shift
      shiftPressed = false;
    }
  });

  var mouseDownPos, gribBoundingBox = null,
    mouseIsDown = 0;
  var themap = map;

  google.maps.event.addListener(themap, 'mousemove', function(e) {
    if (mouseIsDown && shiftPressed) {
      if (gribBoundingBox !== null) // box exists
      {
        bounds.extend(e.latLng);
        gribBoundingBox.setBounds(bounds); // If this statement is enabled, I lose mouseUp events

      } else // create bounding box
      {
        bounds = new google.maps.LatLngBounds();
        bounds.extend(e.latLng);
        gribBoundingBox = new google.maps.Rectangle({
          map: themap,
          bounds: bounds,
          fillOpacity: 0.15,
          strokeWeight: 0.9,
          clickable: false
        });
      }
    }
  });

  google.maps.event.addListener(themap, 'mousedown', function(e) {
    if (shiftPressed) {

      mouseIsDown = 1;
      mouseDownPos = e.latLng;
      themap.setOptions({
        draggable: false
      });
    }
  });

  google.maps.event.addListener(themap, 'mouseup', function(e) {
    if (mouseIsDown && shiftPressed) {
      mouseIsDown = 0;
      if (gribBoundingBox !== null) // box exists
      {
        var boundsSelectionArea = new google.maps.LatLngBounds(gribBoundingBox.getBounds().getSouthWest(), gribBoundingBox.getBounds().getNorthEast());

        for (var key in markers) { // looping through my Markers Collection 

          //                    if (boundsSelectionArea.contains(markers[key].marker.getPosition())) 
          if (gribBoundingBox.getBounds().contains(markers[key].marker.getPosition())) {
            //if(flashMovie !== null && flashMovie !== undefined) {
            markers[key].marker.setIcon("http://maps.google.com/mapfiles/ms/icons/blue.png")
            document.getElementById('info').innerHTML += "key:" + key + " posn:" + markers[key].marker.getPosition() + " in bnds:" + gribBoundingBox.getBounds() + "<br>";
            // console.log("User selected:" + key + ", id:" + markers[key].id);
            //}   
          } else {
            //if(flashMovie !== null && flashMovie !== undefined) {
            markers[key].marker.setIcon("http://maps.google.com/mapfiles/ms/icons/red.png")
            document.getElementById('info').innerHTML += "key:" + key + " posn:" + markers[key].marker.getPosition() + " out of bnds:" + gribBoundingBox.getBounds() + "<br>";
            // console.log("User NOT selected:" + key + ", id:" + markers[key].id);
            //} 
          }
        }

        gribBoundingBox.setMap(null); // remove the rectangle
      }
      gribBoundingBox = null;

    }

    themap.setOptions({
      draggable: true
    });
    //stopDraw(e);
  });

});
#map {
  height: 500px;
  width: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>
<div id="info"></div>

关于javascript - 谷歌地图拖动矩形来选择标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17673580/

相关文章:

javascript - 需要关于如何在 Sequelize 中填充 "through"表的建议?

javascript - 当我在 collapsible-set 中添加代码时。如何在 jquery mobile 中刷新 collapsibleset

通过ajax动态添加内容后Javascript功能不起作用

javascript - 将实际操作应用于右键单击上下文菜单

javascript - 谷歌地图API语法错误

ios - Google API 客户端使用 - API key 是强制性的吗?

javascript - 在 ExpressJS 中向文件路径添加参数

javascript - 如何在不重叠的情况下将绝对定位元素推到底部

jquery - 更新现有 jQuery 对象的选项

javascript - 如何切换标记?