javascript - 按下鼠标时展开谷歌地图圆圈

标签 javascript jquery google-maps-api-3

当用户长按 map 时,我试图画一个圆圈。按住鼠标按钮的时间越长,圆圈就会变大。

然后在鼠标松开时我想停止圆圈的生长并获得其边界。

到目前为止 - http://jsfiddle.net/Ss8xe/1/

new LongPress(map, 500);
google.maps.event.addListener(map, 'longpress', function(e) {
      var radius = 100;
      // Draw a circle around the radius
      var circle = new google.maps.Circle({
        center: e.latLng,
        radius: radius,
        strokeColor: "#0000FF",
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: "#0000FF",
        fillOpacity: 0.4
       });          
       circle.setMap(map);  

    var t;
    var start =  2;
    var speedup =  4;
    var grow = function () {
        radius = radius + 50;
        circle.setRadius(radius);
        t = setTimeout(grow, start);
        start = start / speedup;
    }
    grow();
});

我无法检测到鼠标向上移动以阻止圆圈扩大? “长按”后“mouseup”似乎没有触发

谢谢

最佳答案

就是这样...您需要将 clickable:false 添加到圆圈中,否则圆圈将监听鼠标事件,并且 mouseup 事件将在圆圈上触发,而不是在 map 。

Demo Fiddle

JS:

var map = null;

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(25.435833800555567, -80.44189453125),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoom: 11
    };

    map = new google.maps.Map(document.getElementById("map"), mapOptions);

    new LongPress(map, 500);
    var t;
    google.maps.event.addListener(map, 'longpress', function (e) {
        var radius = 100;
        // Draw a circle around the radius
        var circle = new google.maps.Circle({
            center: e.latLng,
            radius: radius,
            strokeColor: "#0000FF",
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: "#0000FF",
            fillOpacity: 0.4,
            clickable: false
        });
        circle.setMap(map);
        var start = 2;
        var speedup = 4;
        var grow = function () {
            radius = radius + 50;
            circle.setRadius(radius);
            t = setTimeout(grow, start);
            start = start / speedup;
        }
        grow();
    });

    google.maps.event.addListener(map, 'mouseup', function (e) {
         clearTimeout(t);
    });

}

function LongPress(map, length) {
    this.length_ = length;
    var me = this;
    me.map_ = map;
    me.timeoutId_ = null;
    google.maps.event.addListener(map, 'mousedown', function (e) {
        me.onMouseDown_(e);
    });
    google.maps.event.addListener(map, 'mouseup', function (e) {
        me.onMouseUp_(e);
    });
    google.maps.event.addListener(map, 'drag', function (e) {
        me.onMapDrag_(e);
    });
};
LongPress.prototype.onMouseUp_ = function (e) {
    clearTimeout(this.timeoutId_);
};
LongPress.prototype.onMouseDown_ = function (e) {
    clearTimeout(this.timeoutId_);
    var map = this.map_;
    var event = e;
    this.timeoutId_ = setTimeout(function () {
        google.maps.event.trigger(map, 'longpress', event);
    }, this.length_);
};
LongPress.prototype.onMapDrag_ = function (e) {
    clearTimeout(this.timeoutId_);
};

关于javascript - 按下鼠标时展开谷歌地图圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241308/

相关文章:

javascript - 如何在鼠标悬停时创建跨度内容?

google-maps - 对于某些位置,Google 时区 API 返回 ZERO_RESULTS

javascript - 如何在 Angular 组件中手动运行摘要

javascript - 在jsp页面中重新运行测试而不重新加载页面

javascript - 从段落中检索文本并更改其颜色

javascript - 谷歌地图在 Google Map V3 的 mapclick 上显示双标记,但我想要在 mapclick 的特定点上有一个标记

javascript - 如何在 Google Maps API 上定位自定义按​​钮控件?

javascript - 验证 PHP 和 JavaScript 中的表单

javascript - -moz-user-select 在 Javascript 中

jquery Accordion 标题索引