5

下面的代码示例让我感到困惑。这是我可以用来演示我的问题的一段较大代码的最简单版本。基本上我希望用户能够通过按住鼠标键并拖动它来绘制边界框。

如果我在 mouseMove 函数中使用 setBounds 重新绘制矩形,我永远不会看到 mouseUp 事件!如果我在 mouseMove 函数中禁用矩形的重绘,我会得到 mouseUp 事件。我可以通过在 mouseUp 事件中绘制矩形来部分解决这个问题,但是用户在拖动框时看不到轮廓。

您可以在http://www.geoffschultz.org/Test/bounding_box.html看到这个代码示例

这是代码:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
            html { height: 100% }
            body { height: 100%; margin: 0; padding: 0 }
            #mapdiv { height: 100% }
        </style>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
            var map, mouseDownPos, gribBoundingBox = "", mouseIsDown = 0;

        function display_map()
        {       
            var latlng = new google.maps.LatLng(42, -71);
            var myOptions = {zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP};
            map = new google.maps.Map(document.getElementById("mapdiv"), myOptions);

            google.maps.event.addListener(map,'mousemove',function(event) {mouseMove(event);}); 
            google.maps.event.addListener(map,'mousedown',function(event) {mouseDown(event);});
            google.maps.event.addListener(map,'mouseup',function(event) {mouseUp(event);}); 
        }

        function mouseMove(event)
        {
            if (mouseIsDown)
            {
            if (gribBoundingBox) // box exists
                {
                var bounds = new google.maps.LatLngBounds(gribBoundingBox.getBounds().getSouthWest(), event.latLng);
                gribBoundingBox.setBounds(bounds); // If this statement is enabled, I lose mouseUp events
                }
            else // create bounding box
                {
                var bounds = new google.maps.LatLngBounds(mouseDownPos, event.latLng);
                gribBoundingBox = new google.maps.Rectangle({map: map, bounds: bounds, fillOpacity: 0.05, strokeWeight: 1});
                }
            }
        }

        function mouseDown(event)
        {
            mouseIsDown = 1;
            mouseDownPos = event.latLng;
            map.setOptions({draggable: false});
        }

        function mouseUp(event)
        {
            if (mouseIsDown)
            {
                mouseIsDown = 0;
                map.setOptions({draggable: true});
        //      var bounds = new google.maps.LatLngBounds(mouseDownPos, event.latLng);
        //      gribBoundingBox.setBounds(bounds); // If used instead of above, box drawn properly
                gribBoundingBox.setEditable(true);
            }
        }
    </script>
</head>

<body onload="display_map()">
    <div id="mapdiv" style="width:100%; height:100%"></div>
</body>
</html>
4

2 回答 2

2

如果更新矩形边界,该mouseup事件将被矩形消耗(这是因为鼠标光标现在位于矩形内)。有两种解决方案:

  1. clickable矩形对象的属性设置为false。矩形将不会消耗该事件。

    gribBoundingBox = new google.maps.Rectangle({map: map, bounds: bounds, fillOpacity: 0.05, strokeWeight: 1, clickable: false});

  2. mouseup事件侦听器添加到矩形对象。这样,事件由矩形消费和处理。在您的示例中,您可以使用相同的侦听器函数。

    google.maps.event.addListener(gribBoundingBox,'mouseup',function(event) {mouseUp(event);});

于 2011-11-21T18:00:08.083 回答
0

如果其他解决方案不起作用,也可以尝试在文档上注册 mouseup 事件。

document.addEventListener('mouseup', function(evt){
    stopDraw(evt);
});
于 2013-07-05T15:55:19.090 回答