- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个<Map><FeatureGroup><Circle />[1 or more]...</FeatureGroup></Map>
层次结构并使 map 边界适合要素组,以便所有圆圈都在视口(viewport)中。
如果只有一个圆圈,它应该适合该圆圈的边界(即:放大)。
FeatureGroup
一个ref
并调用getBounds
就可以传递到Map
。由于生命周期FeatureGroup
当时不存在componentDidMount
被调用 - 它稍后渲染( https://github.com/PaulLeCam/react-leaflet/issues/106#issuecomment-161594328 )。Circle
状态并对其调用 getBounds(假设在本例中只有一个圆圈。这也不起作用。我想我可能需要对 React Context 做一些事情,但我不确定我现在是否完全理解它,所以我需要一些帮助。
我正在使用react-leaflet@2.1.2
感谢您提供的任何帮助!
最佳答案
由于 Map
的内容在 componentDidMount
时间 ( https://github.com/PaulLeCam/react-leaflet/issues/106#issuecomment-161594328 ) 不可用,因此您无法获取 FeatureGroup
的边界到那时,在您分配的所有 refs
中,只有 Map
ref 在 this.refs
中可用。
但是,根据此 GitHub 评论:https://github.com/PaulLeCam/react-leaflet/issues/106#issuecomment-366263225您可以为 FeatureGroup
提供一个 onAdd
处理函数:
<FeatureGroup ref="features" onAdd={this.onFeatureGroupAdd}>...
然后您可以使用 Map
引用来访问 leafletElement
并使用传入事件目标的边界调用 fitBounds
,这将是FeatureGroup
:
onFeatureGroupAdd = (e) => {
this.refs.map.leafletElement.fitBounds(e.target.getBounds());
}
然后,这将根据需要将 map “缩放”到 FeatureGroup
的范围内。
我修改了我的 React 组件,以便缩放和居中由查询参数控制。上述解决方案的问题在于,例如,如果您通过单击来放大 MarkerClusterGroup
,它会更新 url 中的缩放,重新渲染 map 并重新调用 onFeatureGroupAdd
,从而消除了所有标记簇的优点。
我需要的是访问将新绘制的圆很好地保持在边界内所需的缩放级别,然后使用正确的缩放级别和中心更新网址。
onDrawCircle = (e) => {
...
var targetZoom = this.refs.map.leafletElement.getBoundsZoom(e.layer.getBounds());
// Call function to update url here:
functionToUpdateUrl(targetZoom, e.layer.getBounds().getCenter());
}
}
为了能够控制整个 map ,我还在 onZoomEnd
和 onDragEnd
事件处理程序中调用 functionToUpdateUrl
,如下所示:
onChangeView = (e) => {
functionToUpdateUrl(e.target._zoom, this.refs.map.leafletElement.getCenter());
}
还有一个用于处理集群点击:
onClusterClick = (e) => {
// This time we want the center of the layer, not the map?
functionToUpdateUrl(e.target._zoom, (e.layer ? e.layer.getBounds().getCenter() : e.target.getBounds().getCenter()));
}
然后,在渲染 Map 元素时,传递以下属性:
<Map
center={center}
ref='map'
zoom={zoom}
maxZoom={18}
onZoomEnd={this.onChangeView}
onDragEnd={this.onChangeView}
>
....
</Map>
并记住为任何 MarkerClusterGroup
提供其 onClusterClick
回调:
<MarkerClusterGroup onAdd={this.onMarkerGroupAdd} onClusterClick={this.onClusterClick}>
关于reactjs - 将 map 拟合到 react 传单中的要素图层边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53601692/
我有一个带有 LeafletJS 的项目。 例如,我在 map 中有 2 个点 (A, B)。我将其显示为 2 个标记 我必须画一条从 A 到 B 的折线。 我移动了标记 A,我想将标记 A 的折线的
是否可以向自定义图标标记添加文本?我想避免在图像编辑器中编辑图标只是为了添加文本。 我已经像这样创建了我的自定义图标标记: var airfieldIcon = L.icon({ ic
我想要一个 Leaflet 图层控件,选中/取消选中时必须显示/隐藏主图层内的所有子图层。我还想独立检查/取消选中子层。我浏览了 Leaflet 文档和论坛,但找不到任何引用资料。我应该为此编写一个自
如何处理 Leaflet map 库中的重叠线? 我从服务器 sid 下载 geoJSON 并将其绘制到 map 上。如果有两个相同的条目,Leaflet 会绘制它们两次。这可以通过在服务器端查找完全
我在项目中使用 leafletjs。在 map 上我有几个多边形和标记。当其中一个被点击时,所有这些都有一个点击事件来显示一些信息。如果用户单击 map 的“空白”部分(而不是任何多边形或标记),我想
我正在尝试对下一个边界的外观做出预测。这是一个plunkr: https://plnkr.co/edit/sk6NRh51WZA2vpWP 这就是我让它工作的方式,但它不是很有效: const ori
我是 Leaflet 的新手,目前我正在努力学习教程。到目前为止,我设法创建了一个交互式 clorophet map ,就像示例中的 http://leafletjs.com/examples/cho
我很难弄清楚为什么我无法在 map 上绘制正确的国家/地区。我已经完成了所有代码,但我仍然不明白为什么不能正常工作。 如果您看到任何问题,请告诉我。我很欣赏。 这是数据集 Country
是否可以在使用 Stamen Toner-lite tiles 的 Leaflet map 上设置中间(2.5、3.5、4.5 等)缩放级别? ?这是我到目前为止计算缩放级别的代码: leafletm
早上、下午或晚上。 我有以下位置数据(从'Count of sampling points within a grid cell'调整) # Demo data set.seed(123) # lat
大家好,我正在开发一个具有 map 功能的网站。我的目标是,如果您单击标记,则使其拖动。 这是我的代码 else if (select1.value === "Arson"){ var note =
我正在开发一个应用程序,用户可以在其中上传KML文件,并使用Leaflet,toGeoJSON和Angular-Leaflet-Directive在屏幕上呈现路径。 我使用toGeoJSON.kml(
我需要在多边形要素中实现多色填充。填充将根据要素属性进行有条件的格式化。 假设我需要一个具有 3 色图案的多边形,如下所示: let fillPalette = ['orange', 'green',
我从 javascript 开始,并试图解决这个问题。 我希望当用户单击 map 时出现一个表单。当他们填写表单时,我想在该位置创建一个标记,并将该标记的弹出内容设置为表单中的值。添加表单完成后,我还
我已经在地理服务器中发布了一个功能,我可以通过传单及其 basemap 成功访问该功能。现在我需要在弹出窗口中获取feature onclick的属性信息。 我按原样使用了示例( https://gi
我正在使用带有一些标记的传单 map 。我还有一个侧边栏,可以在其中看到标记的名称,如果单击标记的名称, map 将缩放到标记的 lnglat。它已经可以工作,但我的问题是,当我刷新页面并在侧边栏中选
我正在使用 Leaflet 和 Mapbox,我想设置 map 的 View : 所有标记均可见 中心设置为特定点 使用 setView 和 fitbounds 单独完成每个点很容易,但我不知道如何同
是否有所有潜在 map 源的列表?在示例页面上,可以浏览四种类型的 map 。外面还有什么? http://tombatossals.github.io/angular-leaflet-directi
我正在使用 Leaflet 制作交互式 map 。我在 map 上有标记(基于其经度和纬度坐标),并且我希望某些标记具有与其他标记不同的颜色。例如,某些坐标我给它的分数是“10”,我希望它是粉红色的,
我可能忽略了如何在 map 上更改 LeftletJS 的光标。 http://leafletjs.com/reference.htm 我尝试设置map_div.style.cursor = 'cro
我是一名优秀的程序员,十分优秀!