使用bmap畫我國地圖后並添加需要的數據


官網

一切以官網為主,今天我們需要基於這個地圖 來做。
官網js地址: https://github.com/apache/incubator-echarts/tree/master/dist
這個目錄下有很多js文件,我們選擇echarts.js和extension下的bmap.js, 最好把整個目錄克隆下來,然后復制這2個文件到你的目錄下。

option的參數配置

這里我要介紹下tooltip這個參數,這個參數控制了這個功能:當你的鼠標移動到高亮的那個點時,要想顯示什么內容,就必須通過這個參數來控制顯示內容。如果你什么都配置,那么咱們使用的這個地圖就一直顯示固定內容。

官網文檔地址:https://www.echartsjs.com/zh/option.html#tooltip.formatter

參考官網文檔,我們得知,要想顯示我們需要的內容,就需要通過 formatter 函數來控制返回內容,從而才能夠顯示,那不得不介紹下這formatter 接受的幾個參數了。

formatter: function (params, ticket, callback) { }
  1. params 這個是當前這個元素對象,如,這個對象有value值,那么就 params.value ,如這個對象有name值,那么就params.name ,然后return params.name 就能顯示這個元素name這個值。
  2. ticket 是異步回調標識,配合第三個參數 callback 使用。
  3. callback 是異步回調,在提示框浮層內容是異步獲取的時候,可以通過 callback 傳入上述的 ticket 和 html 更新提示框浮層內容。

代碼先睹為快

前提條件:

后端接口返回的數據必須是以下這樣的

[
  {"name":"贛州市","value":[ 經度,維度, 數值1,數值2  ] },
  {"name":"天津市","value":[ 經度,維度, 數值1,數值2  ] },
]

多說一句,經緯度可以根據百度這個接口來獲取,但是僅供參考,因為有些城市的經緯度不准。
接口地址:http://api.map.baidu.com/geocoder?address=贛州市&output=json

JS前端代碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HSY</title>
    <style>
        .anchorBL {
                display: none;
            }
    </style>
</head>
<body>
    <div  id="user_with_city_div">
            <div id="user_with_city" style="width: 1600px;;height:900px;"></div>
        </div>
</body>
    <script src="http://api.map.baidu.com/api?v=2.0&ak=xxxx自己到百度開發者平台申請"></script>
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script src="/static/js/echarts/echarts.common.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/js/echarts/echarts.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/js/echarts/bmap.js" type="text/javascript" charset="utf-8"></script>
<script>

    function init_graph() {
            var myChart = echarts.init(document.getElementById('user_with_city'));
            myChart.showLoading()
            $.getJSON("/userbi/user/hsy/user_with_city/",{"from_time": {{ from_time }}, "to_time":{{ to_time }} },function(callback){
                //layui.use("layer",function(){
                    //var layer = layui.layer;
                    if(callback.code == 200 ){
                        var city_location_sorted = callback.sorted;
                        var city_location = callback.city_location;
                        myChart.hideLoading()
                        var option = {
                            backgroundColor: 'transparent',
                            title: {
                                text: '?????????',
                                subtext: '?????',
                                sublink: '',
                                left: 'center',
                                textStyle: {
                                    color: '#fff'
                                }
                            },
                            tooltip : {
                                trigger: 'item'
                            },
                            bmap: {
                                center: [112.114129, 30.550339],
                                zoom: 5,
                                roam: true,
                                mapStyle: { 
                                    styleJson: [  // 此處定義的是圖層樣式
                                            {
                                                "featureType": "water",
                                                "elementType": "all",
                                                "stylers": {
                                                    "color": "#044161"
                                                }
                                            },
                                            {
                                                "featureType": "land",
                                                "elementType": "all",
                                                "stylers": {
                                                    "color": "#004981"
                                                }
                                            },
                                            {
                                                "featureType": "boundary",
                                                "elementType": "geometry",
                                                "stylers": {
                                                    "color": "#064f85"
                                                }
                                            },
                                            {
                                                "featureType": "railway",
                                                "elementType": "all",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "highway",
                                                "elementType": "geometry",
                                                "stylers": {
                                                    "color": "#004981"
                                                }
                                            },
                                            {
                                                "featureType": "highway",
                                                "elementType": "geometry.fill",
                                                "stylers": {
                                                    "color": "#005b96",
                                                    "lightness": 1
                                                }
                                            },
                                            {
                                                "featureType": "highway",
                                                "elementType": "labels",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "arterial",
                                                "elementType": "geometry",
                                                "stylers": {
                                                    "color": "#004981"
                                                }
                                            },
                                            {
                                                "featureType": "arterial",
                                                "elementType": "geometry.fill",
                                                "stylers": {
                                                    "color": "#00508b"
                                                }
                                            },
                                            {
                                                "featureType": "poi",
                                                "elementType": "all",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "green",
                                                "elementType": "all",
                                                "stylers": {
                                                    "color": "#056197",
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "subway",
                                                "elementType": "all",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "manmade",
                                                "elementType": "all",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "local",
                                                "elementType": "all",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "arterial",
                                                "elementType": "labels",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            },
                                            {
                                                "featureType": "boundary",
                                                "elementType": "geometry.fill",
                                                "stylers": {
                                                    "color": "#029fd4"
                                                }
                                            },
                                            {
                                                "featureType": "building",
                                                "elementType": "all",
                                                "stylers": {
                                                    "color": "#1a5787"
                                                }
                                            },
                                            {
                                                "featureType": "label",
                                                "elementType": "all",
                                                "stylers": {
                                                    "visibility": "off"
                                                }
                                            }
                                    ]
                                }
                            },
                            series : [
                                    {
                                        name: 'UV',
                                        type: 'scatter',
                                        coordinateSystem: 'bmap',
                                        data:  city_location , //convertData(city_uv,city_location),
                                        tooltip: {
                                            formatter: function (params, ticket, callback) {
                                                     //params  this is your data
                                                    return "UV:"+params.value[2]+"<br>"+"PV:"+params.value[3]
                                                },
                                        },
                                        symbolSize: function (val) {
                                            return val[2] / 5 ;
                                        },
                                        label: {
                                            formatter: '{b}',
                                            position: 'right'
                                        },
                                        itemStyle: {
                                            color: '#ddb926'
                                        },
                                        emphasis: {
                                            label: {
                                                show: true
                                            }
                                        }
                                    },
                                    {
                                        name: 'Top 10',
                                        type: 'effectScatter',
                                        coordinateSystem: 'bmap',
                                        data: city_location_sorted,
                                        tooltip: {
                                            formatter: function (params, ticket, callback) {  // 此處定義的鼠標移動到高亮點以后顯示的內容。
                                            // params 就是咱們后端返回的一行數據,如{"name":"天津市","value":[ 經度,維度, 數值1,數值2  ] }
                                                     //params  this is your data
                                                    return "TOP 10  ??<br>UV:"+params.value[2]+"<br>"+"PV:"+params.value[3]
                                                },
                                        },
                                        symbolSize: function (val) {
                                            return val[2] / 5 ;  // 高亮點縮小5倍
                                        },
                                        showEffectOn: 'emphasis',
                                        rippleEffect: {
                                            brushType: 'stroke'
                                        },
                                        hoverAnimation: true,
                                        label: {
                                            formatter: '{b}',
                                            position: 'right',
                                            show: true
                                        },
                                        itemStyle: {
                                            color: '#f4e925',
                                            shadowBlur: 10,
                                            shadowColor: '#333'
                                        },
                                        zlevel: 1
                                    },

                                ]
                        };
                        myChart.setOption(option);
                        var bmap = myChart.getModel().getComponent('bmap').getBMap();  // 這里的getComponent里面的bmap千萬不要寫錯了,如果提示找不到bmap或者undefined沒有getBMAP屬性,那你就要檢查下自己在上面option里面有沒有寫正確對於bmap這四個字。
                        bmap.addControl(new BMap.MapTypeControl());
                    }else{
                        layer.msg("server error ! can't get the data [ uv with city ]",{icon:5,anim:6})
                    }
                //})
            })
        }
    init_graph()
</script>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM