首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Maps Rectangle Rectangle:如何锁定(固定)高度以防止编辑

Google Maps Rectangle Rectangle:如何锁定(固定)高度以防止编辑
EN

Stack Overflow用户
提问于 2014-09-05 19:53:34
回答 1查看 745关注 0票数 5

我有一个谷歌地图,在它是一个矩形,这是可编辑的,可移动的和大小可调等。我正在寻找的是一种方法来锁定矩形的给定高度,所以只有宽度可以改变。

EN

回答 1

Stack Overflow用户

发布于 2014-09-17 11:07:49

您可以使用JavaScript中的bounds_changed事件防止矩形调整高度。

下面是一个有效的jsfiddle:http://jsfiddle.net/h7ee4368/

矩形接口参考:https://developers.google.com/maps/documentation/javascript/reference?hl=de#Rectangle

JavaScript源:

代码语言:javascript
复制
var rectangle;
var originalBounds;
var map;

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(44.5452, -78.5389),
        zoom: 9
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var bounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(44.490, -78.649),
        new google.maps.LatLng(44.599, -78.443)
    );

    originalBounds = bounds;

    // Define the rectangle and set its editable property to true.
    rectangle = new google.maps.Rectangle({
        bounds: bounds,
        editable: true,
        draggable: true
    });

    rectangle.setMap(map);

    // Add an event listener on the rectangle.
    google.maps.event.addListener(rectangle, 'bounds_changed', boundsChangedCb);
}

var skipBoundsChangedCb = false;
function boundsChangedCb(event) {
    if(skipBoundsChangedCb) return;

    var ne = rectangle.getBounds().getNorthEast();
    var sw = rectangle.getBounds().getSouthWest();

    var origNe = originalBounds.getNorthEast();
    var origSw = originalBounds.getSouthWest();

    //avoid recursion
    skipBoundsChangedCb = true;

    rectangle.setBounds(new google.maps.LatLngBounds(
        new google.maps.LatLng(ne.lat() - origNe.lat() + origSw.lat(), sw.lng()),
        ne        
    ));

    skipBoundsChangedCb = false;

    origNe = ne;
    origSw = sw;
}

google.maps.event.addDomListener(window, 'load', initialize);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25685320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档