写一个react组件,带开关的mapbox点击测距

时间: 2024-05-02 13:23:04 浏览: 10
好的,我可以帮你回答这个问题。以下是一个示例React组件,它可以在Mapbox地图上使用点击事件测距离,并带有开关: ``` import React, { Component } from 'react'; import mapboxgl from 'mapbox-gl'; mapboxgl.accessToken = 'your_access_token_here'; class Map extends Component { constructor(props) { super(props); this.state = { map: null, distance: 0, isMeasuring: false }; this.handleMapClick = this.handleMapClick.bind(this); this.toggleMeasurement = this.toggleMeasurement.bind(this); } componentDidMount() { const map = new mapboxgl.Map({ container: this.mapContainer, style: 'mapbox://styles/mapbox/streets-v11', center: [-122.4443, 47.2529], zoom: 10 }); map.on('click', this.handleMapClick); this.setState({ map }); } handleMapClick(event) { const { isMeasuring, map } = this.state; if (isMeasuring) { const { lngLat } = event; const { lng, lat } = lngLat; const { lng: prevLng, lat: prevLat } = map.getSource('measure')._data.geometry.coordinates[ map.getSource('measure')._data.geometry.coordinates.length - 1 ]; const newDistance = this.calculateDistance([prevLng, prevLat], [lng, lat]); this.setState({ distance: this.state.distance + newDistance }); const source = map.getSource('measure'); source.setData({ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [...source._data.geometry.coordinates, [lng, lat]] } }); } } calculateDistance(point1, point2) { const lng1 = point1[0]; const lat1 = point1[1]; const lng2 = point2[0]; const lat2 = point2[1]; const R = 6371e3; const φ1 = lat1 * (Math.PI / 180); const φ2 = lat2 * (Math.PI / 180); const Δφ = ((lat2 - lat1) * Math.PI) / 180; const Δλ = ((lng2 - lng1) * Math.PI) / 180; const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; } toggleMeasurement() { const { isMeasuring, map } = this.state; if (isMeasuring) { this.setState({ isMeasuring: false }); map.removeSource('measure'); map.removeLayer('measure-line'); } else { this.setState({ isMeasuring: true, distance: 0 }); map.addSource('measure', { type: 'geojson', data: { type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [] } } }); map.addLayer({ id: 'measure-line', type: 'line', source: 'measure', layout: { 'line-join': 'round', 'line-cap': 'round' }, paint: { 'line-color': '#0000ff', 'line-width': 4 } }); } } render() { const { distance, isMeasuring } = this.state; return ( <div> <div ref={el => (this.mapContainer = el)} className="map-container" /> <div> <button onClick={this.toggleMeasurement}>{isMeasuring ? 'Stop Measuring' : 'Start Measuring'}</button> <div>Distance: {distance.toFixed(2)} meters</div> </div> </div> ); } } export default Map; ``` 请注意,此代码仅为示例,如果您想在实际应用中使用此代码,请根据您的需求进行修改。同时,您需要在Mapbox网站上获取自己的访问令牌并将其添加到`mapboxgl.accessToken`中。

相关推荐

最新推荐

recommend-type

在React 组件中使用Echarts的示例代码

本篇文章主要介绍了在React 组件中使用Echarts的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

React如何将组件渲染到指定DOM节点详解

主要给大家介绍了关于React如何将组件渲染到指定DOM节点的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习下吧。
recommend-type

用react-redux实现react组件之间数据共享的方法

主要介绍了用react-redux实现react组件之间数据共享的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

从零开始搭建一个react项目开发

主要介绍了从零开始搭建一个react项目开发,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

详解react如何在组件中获取路由参数

主要介绍了详解react如何在组件中获取路由参数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。