顺琦自然 發表於 2020-9-18 16:13:00

react项目结合echarts,百度地图实现热力图

<p><strong>一.最近在一个react项目(antd pro)中需要展示一个热力地图。需求是:</strong></p>
<p>1.热力地图可缩放;</p>
<p>2.鼠标点击可以展示该点地理坐标,及热力值。</p>
<p>3.初始化时候自适应展示所有的热力点。</p>
<p>4.展示热力标尺。</p>
<p><img src="https://img2020.cnblogs.com/blog/1649912/202009/1649912-20200918153701570-974005474.png"></p>
<p><strong>二.在实现的过程中出现了不少的问题.</strong></p>
<p>引入地图出现问题<br>1.缩放地图的时候中心点会漂移。<br>&nbsp; &nbsp;解决方案:<br>&nbsp; &nbsp; &nbsp; a.把地图放到页面的顶部。<br>&nbsp; &nbsp; &nbsp; b.每次缩放后获取地图中心点,再次设置中心点。关键代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">   let cp;
   bmap.addEventListener(</span>"mousemove",<span style="color: rgba(0, 0, 255, 1)">function</span>(){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载完成时,触发</span>
       cp =<span style="color: rgba(0, 0, 0, 1)"> bmap.getCenter();
   });
   bmap.addEventListener(</span>"mouseend",<span style="color: rgba(0, 0, 255, 1)">function</span>(){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载完成时,触发</span>
       cp =<span style="color: rgba(0, 0, 0, 1)"> bmap.getCenter();
   });
   bmap.addEventListener(</span>"tilesloaded",<span style="color: rgba(0, 0, 255, 1)">function</span>(){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载完成时,触发</span>
<span style="color: rgba(0, 0, 0, 1)">       bmap.setCenter(cp);
   });</span></pre>
</div>
<p>2.热力图标尺不展示。<br>&nbsp; &nbsp;解决方案:<br>&nbsp; &nbsp; &nbsp; &nbsp;a.将百度地图引入echanrts图表中,通过配置项加热力标尺。(visualMap属性)</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;b.手写标尺放入百度地图页面。(可以自己尝试弄,这里我不介绍)</p>
<p>3.热力地图的点不能点击。&nbsp;</p>
<p>&nbsp; &nbsp;解决方案:创建标注(这个标注可能有点丑,如果不想要,可以用透明的图片替代)</p>
<p>&nbsp; &nbsp;这有篇博客写的很详细,https://blog.csdn.net/zjuwwj/article/details/53374947#commentsedit</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个标注</span>
   <span style="color: rgba(0, 0, 255, 1)">var</span> point = <span style="color: rgba(0, 0, 255, 1)">new</span> BMap.Point(data, data);
   </span><span style="color: rgba(0, 0, 255, 1)">var</span> marker = <span style="color: rgba(0, 0, 255, 1)">new</span> BMap.Marker(point);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建标注</span>
   bmap.addOverlay(marker);</pre>
</div>
<p>4.百度地图默认不会展示目标点的地理坐标(本来引入echarts,想着直接用tooltip属性,结果不生效,查找原因发现热力点是个映射关系,不是实实在在的坐标点,获取不到坐标信息)。<br>&nbsp; &nbsp; 解决方案: 获取目标点地理坐标,手动加入悬浮层。</p>
<p>5.初始化时候需要自适应展示所有的热力点(打开地图的时候有默认的比例尺,或者自己设定的固定的比例尺。)</p>
<p>&nbsp; &nbsp;解决方案:计算所有热力点中最远的两个的距离,我是设定数据的第一个点为中心点,计算此点与其他所有点的距离,找出最大值,然后对比百度地图等级设定。</p>
<p>百度地图对应的等级可参考:http://api.map.baidu.com/lbsapi/getpoint/index.html</p>
<p>综合实现这些东西,可费了不少劲呢。</p>
<p><strong>三.先来看看热力地图代码:heatMap.js</strong></p>
<p>这是在react项目里使用了echarts和百度地图。</p>
<div class="cnblogs_code">
<pre>// heatMap.js<br><br>import React, { Component } from 'react'<span style="color: rgba(0, 0, 0, 1)">;

import echarts from </span>'echarts/lib/echarts'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入热力图</span>
import 'echarts/lib/chart/heatmap'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入提示框和标题组件</span>
import 'echarts/lib/component/tooltip'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'echarts/lib/component/title'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'echarts/extension/bmap/bmap'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'echarts/lib/component/visualMap'<span style="color: rgba(0, 0, 0, 1)">;

class EchartsTest extends Component {
constructor(props) {
    super(props);
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
      data: props.data,
    };
}

componentDidMount() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getCharts();
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 保证再次请求数据时候,地图重新加载</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-unused-vars</span>
<span style="color: rgba(0, 0, 0, 1)">componentWillReceiveProps(nextProps, nextContext) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
      data: nextProps.data,
    });
    setTimeout(() </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!nextProps.data.length===0<span style="color: rgba(0, 0, 0, 1)">){
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getCharts();
      }
    }, </span>500<span style="color: rgba(0, 0, 0, 1)">);
}

getCharts </span>= () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    const { data } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state;<br>   // 这里data格式:[,] ,里面是经度,维度,热力值
    const maxdata </span>=<span style="color: rgba(0, 0, 0, 1)"> data
      .map(item </span>=&gt; item)
      .sort()
      .reverse()[</span>0<span style="color: rgba(0, 0, 0, 1)">];
    const myChart </span>= echarts.init(document.getElementById('main'<span style="color: rgba(0, 0, 0, 1)">));
    const option </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      animation: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> tooltip: { // 悬浮层提示框在热力图上无效</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   trigger: 'item',</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   triggerOn: 'click',</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> },</span>
<span style="color: rgba(0, 0, 0, 1)">      bmap: {
      center: , data],
      zoom: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getMapGrade(data),
      roam: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      },
      visualMap: {
      show: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> top: 'top',</span>
      bottom: 50<span style="color: rgba(0, 0, 0, 1)">,
      left: </span>0<span style="color: rgba(0, 0, 0, 1)">,
      min: </span>0<span style="color: rgba(0, 0, 0, 1)">,
      max: maxdata,
      seriesIndex: </span>0<span style="color: rgba(0, 0, 0, 1)">,
      calculable: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      inRange: {
          color: [</span>'blue', 'green', 'yellow', 'red'<span style="color: rgba(0, 0, 0, 1)">],
      },
      },
      series: [
      {
          name: </span>'gid热力值'<span style="color: rgba(0, 0, 0, 1)">,
          type: </span>'heatmap'<span style="color: rgba(0, 0, 0, 1)">,
          coordinateSystem: </span>'bmap'<span style="color: rgba(0, 0, 0, 1)">,
          data,
          pointSize: </span>8<span style="color: rgba(0, 0, 0, 1)">,
          blurSize: </span>8<span style="color: rgba(0, 0, 0, 1)">,
      },
      ],
    };
    myChart.setOption(option);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加百度地图插件</span>
    const bmap =<span style="color: rgba(0, 0, 0, 1)"> myChart
      .getModel()
      .getComponent(</span>'bmap'<span style="color: rgba(0, 0, 0, 1)">)
      .getBMap();

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 解决地图放大地图中心点漂移的问题。当地图不在页面顶部使用</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> let cp;</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> bmap.addEventListener("mousemove",function(){ // 加载完成时,触发</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   cp = bmap.getCenter();</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> });</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> bmap.addEventListener("mouseend",function(){ // 加载完成时,触发</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   cp = bmap.getCenter();</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> });</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> bmap.addEventListener("tilesloaded",function(){ // 加载完成时,触发</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   bmap.setCenter(cp);</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> });</span>

    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
    bmap.addControl(<span style="color: rgba(0, 0, 255, 1)">new</span> BMap.NavigationControl()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 地图平移缩放控件</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
    bmap.addControl(<span style="color: rgba(0, 0, 255, 1)">new</span> BMap.ScaleControl()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 地图比例尺控件</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个标注</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> var point = new BMap.Point(data, data);</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> var marker = new BMap.Marker(point);// 创建标注</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> bmap.addOverlay(marker);</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-plusplus</span>
    <span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; data.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
      const hotPoint = <span style="color: rgba(0, 0, 255, 1)">new</span> BMap.Point(data, data);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
      const marker = <span style="color: rgba(0, 0, 255, 1)">new</span> BMap.Marker(hotPoint); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建标注</span>
      bmap.addOverlay(marker); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将标注添加到地图中</span>

      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-use-before-define</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> marker.addEventListener("click",getAttr);</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> function getAttr(){</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   var p = marker.getPosition();       // 获取marker的位置</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   p.id = "123";</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   console.log("点的位置是" + hotPoint.lng + "," + hotPoint.lat);</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   console.log("marker的位置是" + p.lng + "," + p.lat);</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }</span>

      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
      const infoWindow = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BMap.InfoWindow(`
      </span>&lt;div style="margin:0;line-height:20px;padding:2px;"&gt;<span style="color: rgba(0, 0, 0, 1)">
          标题:热点详细信息
          </span>&lt;br/&gt;地理位置:${data}, ${data}
          &lt;br/&gt;最近三个月热力值:${data}
      &lt;/div&gt;`);
<span style="color: rgba(0, 0, 0, 1)">
      marker.infoWindow </span>= infoWindow; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 给当前标注新增一个属性以便保存窗口信息infoWindow</span>
      marker.addEventListener('click', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e) {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.openInfoWindow(e.target.infoWindow); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点击标注时,打开改标注对打开改标注对应的回调信息</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果使用下面的方式,那样就会导致每次标注点击后,弹出的窗口信息都是最后一次循环的infoWindow。因为在click的时候只会去找infoWindow这个变量值,而你的click肯定是在所有循环的,标注都产生完之后,此时infoWindow变量已经被赋值成了最后一次循环的值。</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> this.openInfoWindow(infoWindow);</span>
<span style="color: rgba(0, 0, 0, 1)">      });
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
<span style="color: rgba(0, 0, 0, 1)">    bmap.addControl(
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
      <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BMap.MapTypeControl({
      mapTypes: [
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-undef</span>
<span style="color: rgba(0, 0, 0, 1)">          BMAP_NORMAL_MAP,
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> BMAP_HYBRID_MAP</span>
<span style="color: rgba(0, 0, 0, 1)">      ],
      })
    );
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 去掉上面的{mapTypes:[...]}就会显示地图,卫星,三维三个图层</span>
<span style="color: rgba(0, 0, 0, 1)">};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 计算经纬度距离(千米),四个参数分别是点A的纬度,经度,点B的纬度,经度(位置不要搞错了,我就弄错了,搞了好久)</span>
getDistance =(lat1, lng1, lat2, lng2)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    const radLat1 </span>= lat1*Math.PI / 180.0<span style="color: rgba(0, 0, 0, 1)">;
    const radLat2 </span>= lat2*Math.PI / 180.0<span style="color: rgba(0, 0, 0, 1)">;
    const a </span>= radLat1 -<span style="color: rgba(0, 0, 0, 1)"> radLat2;
    const b </span>= lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-restricted-properties</span>
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-restricted-properties</span>
      Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s *=6378.137<span style="color: rgba(0, 0, 0, 1)"> ;
    s </span>= Math.round(s * 10000) / 10000<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> s;
};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 计算地图初始化所有地理坐标中距离最大值</span>
getZoom = (val)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
    const arr </span>=<span style="color: rgba(0, 0, 0, 1)"> [];
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(val.length===1<span style="color: rgba(0, 0, 0, 1)">){
      arr.push(</span>1<span style="color: rgba(0, 0, 0, 1)">)
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
      </span><span style="color: rgba(0, 0, 255, 1)">for</span>(let i=1;i&lt;val.length;i+=1<span style="color: rgba(0, 0, 0, 1)">){
      arr.push(</span><span style="color: rgba(0, 0, 255, 1)">this</span>.getDistance(val,val,val,val))
      }
    }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log("所有距离",arr);</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Math.max(...arr)
};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 计算比例尺对应的百度地图等级</span>
getMapGrade=(val)=&gt;<span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log("数据 ",val);</span>
    const num=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getZoom(val);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   console.log("最大距离",num);</span>
    let zoom=0<span style="color: rgba(0, 0, 0, 1)">;
   </span><span style="color: rgba(0, 0, 255, 1)">if</span>(num&lt;=1<span style="color: rgba(0, 0, 0, 1)">){
   zoom</span>=15<span style="color: rgba(0, 0, 0, 1)">
   }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(num&gt;1&amp;&amp;num&lt;=50<span style="color: rgba(0, 0, 0, 1)">){
   zoom</span>=10<span style="color: rgba(0, 0, 0, 1)">
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(num&gt;50&amp;&amp;num&lt;=100<span style="color: rgba(0, 0, 0, 1)">){
   zoom</span>=9<span style="color: rgba(0, 0, 0, 1)">
   }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(num&gt;100&amp;&amp;num&lt;=500<span style="color: rgba(0, 0, 0, 1)">){
   zoom</span>=7<span style="color: rgba(0, 0, 0, 1)">
   }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(num&gt;500&amp;&amp;num&lt;=1000<span style="color: rgba(0, 0, 0, 1)">){
   zoom</span>=6<span style="color: rgba(0, 0, 0, 1)">
   }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
   zoom</span>=4<span style="color: rgba(0, 0, 0, 1)">
   }
   </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> zoom
};


render() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div id="main" style={{ width: '100%', height: 600 }}&gt;&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> EchartsTest;</pre>
</div>
<p><strong>四.引用</strong></p>
<p>1.在项目的入口html文件中引入:</p>
<p>需要自己申请百度地图的密钥,直接用下面的密钥也可以:</p>
<div class="cnblogs_code">
<pre>&lt;script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&amp;ak=Imejyag6D5IPg4lOfu0LiDUWBGh2SNmc"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/extension/bmap.min.js"&gt;&lt;/script&gt;</pre>
</div>
<p>2.在自己页面中引入heatMap.js</p>
<div class="cnblogs_code">
<pre>import HeatMap from './heatMap'<span style="color: rgba(0, 0, 0, 1)">;//引入自己写好的热力地图<br>const arry=[<br>,<br>,<br>];         
</span>&lt;HeatMap data={arry} /&gt;   //直接在页面引入标签使用arry的数据格式你可以有自己的格式,不过你的格式改了的话,heatMap.js参数使用也得自己改下。</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/class1/p/13691867.html
頁: [1]
查看完整版本: react项目结合echarts,百度地图实现热力图