jquery - 如何指定传单 map 的 "active"区域?

标签 jquery openstreetmap leaflet

请考虑:http://jsfiddle.net/msNs5/3/

我有一个 map div,在页面加载时会扩展为全尺寸,另一个半透明 div 覆盖其底部。从美学上来说,我真的很喜欢底部 div 与它后面的 map 的外观,但我需要使其未被透明 div 覆盖的区域成为“事件”区域。

即本地图计算标记和多边形的边界并设置其 View 时,它会计算该暴露区域而不是整个 map 本身。人们似乎认为这是可能的,但我在实际实现中遇到了困难。

fiddle 代码如下:

// JavaScript
var map = L.map('map', { center: [51.505, -0.09], zoom: 13 });
osm = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {});
map.addLayer(osm);

function initMap() {
  $('#map').css({"height": $(window).height() + "px"});
  map.setView(map.getCenter(),map.getZoom());
}

var id;
$(window).resize(function() {
  clearTimeout(id);

  id = setTimeout(initMap, 500);
});

$('#map').css({"height": $(window).height() + "px"});
  map.setView(map.getCenter(),map.getZoom());

/* CSS */
#map { position: fixed; z-index: -1; width: 100%; }
#header { height: 40px; background: black; }
#otherstuff { position: fixed; bottom: 0px; width: 100%; background-color: rgba(255,255,255,0.6); height: 30%; }

<!-- HTML --->
<!DOCTYPE html>
<body>
    <div id="header"></div>
    <div id="map"></div>

    <div id="otherstuff">xxx</div>
</body>

最佳答案

感谢 #leaflet channel 上的 @mourner,我被引导至此 Github 问题主题,特别是此评论:

https://github.com/Leaflet/Leaflet/issues/859#issuecomment-11436202

它定义了一个用于偏移 map 中心的mixin:

MapCenterOffsetMixin = {
    UIOffset: [550, 0], // x, y
    getBounds: function(){
        var a=this.getPixelBounds(),
            b=this.unproject(new L.Point(a.min.x+this.UIOffset[0],a.max.y+this.UIOffset[1]), this._zoom,!0),
            c=this.unproject(new L.Point(a.max.x,a.min.y),this._zoom,!0);
            return new L.LatLngBounds(b,c)
    },
    _latLngToNewLayerPoint: function (latlng, newZoom, newCenter) {
        var targetPoint = this.project(newCenter, newCenter).subtract([this.UIOffset[0]/2, this.UIOffset[1]/2]),
            newCenter = this.unproject(targetPoint, newZoom);
        var topLeft = this._getNewTopLeftPoint(newCenter, newZoom).add(this._getMapPanePos());
        return this.project(latlng, newZoom)._subtract(topLeft);
    },
    _getCenterLayerPoint: function () {
        return this.containerPointToLayerPoint(this.getSize().divideBy(2).add([this.UIOffset[0]/2, this.UIOffset[1]/2]));
    },
    _resetView: function (a, b, c, d) {
        var e = this._zoom !== b;
        // Change the center
        var targetPoint = this.project(a, b).subtract([this.UIOffset[0] / 2, this.UIOffset[1]/2]),
            a = this.unproject(targetPoint, b);
        d || (this.fire("movestart"), e && this.fire("zoomstart")), this._zoom = b, this._initialTopLeftPoint = this._getNewTopLeftPoint(a);
        if (!c) L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
        else {
            var f = L.DomUtil.getPosition(this._mapPane);
            this._initialTopLeftPoint._add(f)
        }
        this._tileLayersToLoad = this._tileLayersNum, this.fire("viewreset", {
            hard: !c
        }), this.fire("move"), (e || d) && this.fire("zoomend"), this.fire("moveend"), this._loaded || (this._loaded = !0, this.fire("load"))
    }
}

L.Map.include(MapCenterOffsetMixin);

这对我来说非常有效。

关于jquery - 如何指定传单 map 的 "active"区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15479786/

相关文章:

python - 单击传单 Web 应用程序后如何将纬度和经度返回到 Flask 服务器?

asp.net - 从服务器端的 asp.net 获取 jquery Accordion Pane 的事件索引?

javascript - 在 Wordpress 插件中包含 jQuery

javascript - 将 uMap 导入 Leaflet

google-maps - 在 Google map 上显示 OpenStreetMap 边界(使用 v3 api)

javascript - 从 CSV 将 highcharts 添加到 mapbox 弹出窗口?

javascript - Jquery点击函数,同一个按钮显示不同的文字

javascript - 点击问题的 Accordion

javascript - D3.js 错误绘制geojson

javascript - 传单嘈杂的控制台输出