简体   繁体   English

传单放大到多边形

[英]leaflet zoom in to polygon

semi-new to leaflet & mapbox. 对传单和地图框是半新的。 I have a polygon on my map that, on rollover, gains a stroke by way of another polygon being added to the map. 我的地图上有一个多边形,翻转时会通过将另一个多边形添加到地图中来获得笔触。 This is kind of glitchy(the stroked polygon shows up if you move your mouse on to the polygon and don't move it, but if you move the mouse at all within the polygon, the stroked polygon flashes on and off. so I'd be happy to hear another way to accomplish this without that happening! My main issue is I want the map to zoom in to the boundaries-ish of the polygon (or center on the polygon & I set the view) when the user clicks the polygon. 这是一种小故障(如果将鼠标移到多边形上而不移动它,则会显示描边多边形,但是如果您在多边形内完全移动鼠标,则描边多边形会不断闪烁。所以我d很高兴听到另一种方法来完成此任务,而这没有发生!我的主要问题是当用户单击“地图”时,我希望地图放大到多边形的边界(或多边形的中心并设置视图)。多边形。

The event listener for clicking the polygon is at the bottom. 单击多边形的事件侦听器在底部。 I've tried a bunch of different syntax including: 我尝试了很多不同的语法,包括:

// the original polygon is the one listening now (maybe they should both have event listeners?)
dtPolygon.addEventListener("click", function(){
    dealMap.fitBounds(polygonPoints);
;});

dtPolygon.addEventListener("click", function(){
    dealMap.fitBounds(dtPolygon.getbounds());
;});

dtPolygon.addEventListener("click", function(){
    dealMap.fitBounds(polygon2.getbounds());
;});

// and just other ideas for what could work, switching out the dtPolygon & polygon 2, which is the "hover" polygon that shows up.


//actual polygon code & implementation
var p1 = new L.LatLng(35.600449, -82.562839),
        p2 = new L.LatLng(35.603380, -82.557517),
        p3 = new L.LatLng(35.602996, -82.546703),
        p4 = new L.LatLng(35.598290, -82.544061),
        p5 = new L.LatLng(35.591574, -82.541886),
        p6 = new L.LatLng(35.588481, -82.543066),
        p7 = new L.LatLng(35.588481, -82.543066),
        p8 = new L.LatLng(35.588073, -82.552910),
        p9 = new L.LatLng(35.588828, -82.561375),
        p10 = new L.LatLng(35.595842, -82.563006),
        polygonPoints = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10];

    var polygonOptions = {
                    stroke: false, 
                     fillColor: 'green', 
                     fillOpacity: 0.5
    };

    var dtPolygon = new L.Polygon(polygonPoints, polygonOptions).addTo(dealMap);

    var polygon2 = new L.Polygon(polygonPoints, {color: 'green', stroke: true});
    polygon2.bringToFront()

    dtPolygon.addEventListener("mouseover", function(){
        polygon2.addTo(dealMap);
    ;});

    dtPolygon.addEventListener("mouseout", function(){
        polygon2.remove(dealMap);
    ;});



    polygon2.addEventListener("click", function(){
        dealMap.fitBounds(polygonPoints);
    ;});

To highlight a polygon on mouseover you can use setStyle() , you don't need to create a copy of the polygon: 要在鼠标悬停时突出显示多边形,可以使用setStyle() ,而无需创建多边形的副本:

dtPolygon.on('mouseover', function(evt){
  evt.target.setStyle({
    stroke: true
  })
})

dtPolygon.on('mouseout', function(evt){
  evt.target.setStyle(polygonOptions)
})

I guess your "click to zoom" didn't work properly because polygon2 kept moving between foreground and background, and that's why it is flickering. 我猜您的“单击缩放”无法正常工作,因为polygon2一直在前景和背景之间移动,这就是为什么它会闪烁。 Anyways, you can remove polygon2 altogether. 无论如何,您可以完全删除polygon2

dtPolygon.on('click', function(evt){
  dealMap.fitBounds(evt.target.getBounds())
})

Demo on Plunker 在Plunker上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM