webgis开发之mapbox(城市天气查询)

MAPBOX

主页面代码

draft2.html

 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>drafts</title>
    <!-- 1、引入mapbox库以及它的样式 -->
    <script src="lib/mapbox.js"></script>
    <link rel="stylesheet" href="lib/mapbox.css">
    <script src="lib/turf.js"></script>
    <script src="lib/jquery.min.js"></script>
    <script
        src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.0/mapbox-gl-geocoder.min.js"></script>
    <link rel="stylesheet"
        href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.0/mapbox-gl-geocoder.css"
        type="text/css" />
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        .header {
            background-color: rgb(76, 158, 255);
            box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.1);
            width: 26vw;
            height: 50px;
            padding: 0px 10px;
            margin: 3px;
            position: fixed;
            top: 0;
            z-index: 2;
            display: flex;
            justify-content: space-around;
            align-items: center;
            border-radius: 5px;
        }

        .header_select {
            width: 20%;
            height: 50px;
            line-height: 50px;
            color: white;
            text-align: center;
            cursor: pointer;
            transition: background-color 0.3s;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            /* 添加颜色变化的过渡效果 */
        }

        /* 鼠标悬浮时的效果 */
        .header_select:hover {
            background-color: rgba(0, 115, 255);
        }

        /* 按下去的感觉 */
        .header_select:active {
            background-color: rgba(0, 81, 180);
        }

        /* 修改链接样式 */
        .header_select a {
            text-decoration: none;
            color: inherit;
        }

        .header_div_i {
            width: 80%;
            height: 68%;
            background-color: #fff;
            box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.1);
            border: none;
            border-radius: 5px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .header_div_input {
            width: 80%;
            height: 80%;
            border: none;
            outline: none;
            word-spacing: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .weatherCondition {
            width: 20vw;
            border-radius: 8px;
            overflow: hidden;
            background-color: rgba(76, 158, 255, 0.25);
            /* background-color: rgba(255, 219, 0, 0.5); */
            border: 0.5px dashed black;
            position: fixed;
            right: 5px;
            top: 5px;
            z-index: 1;
        }

        /* 设置表格样式 */
        .weatherTable {
            border-collapse: collapse;
            width: 100%;
            border-radius: 4px;
        }

        /* 设置表格行样式 */
        .weatherRow {
            border-bottom: 1.2px solid #f5f3f3;
        }

        /* 设置表格数据样式 */
        .weatherData {
            padding: 8px;
            text-align: center;
            font-size: 12px;
        }

        .c1 {
            width: 30%;
            border-right: 1.2px solid #f5f3f3;
        }

        .c2 {
            width: 70%;
        }
    </style>
</head>

<body>
    <div class="header">
        <div class="header_select" onclick="location.href='http://127.0.0.1:5500/map/drafts3.html'">CITY</div>
        <div class="header_div_i">
            <svg t="1706109836956" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
                p-id="6986" width="20%" height="80%">
                <path
                    d="M417.05 770.27c-94.33 0-183.01-36.73-249.7-103.43-137.69-137.68-137.69-361.72 0-499.4C234.05 100.74 322.73 64 417.05 64s183 36.74 249.7 103.43c137.69 137.68 137.69 361.72 0 499.4-66.69 66.71-155.37 103.44-249.7 103.44z m0-642.06c-77.18 0-149.73 30.06-204.3 84.62-112.65 112.66-112.65 295.95 0 408.61 54.57 54.57 127.13 84.62 204.3 84.62 77.17 0 149.73-30.05 204.3-84.62C734 508.78 734 325.49 621.35 212.83c-54.57-54.57-127.13-84.62-204.3-84.62z"
                    fill="#8a8a8a" p-id="6987"></path>
                <path
                    d="M905.1 960c-8.51 0-16.68-3.39-22.7-9.41L610 678.18c-12.54-12.53-12.54-32.86 0-45.4l22.7-22.7c12.52-12.54 32.86-12.54 45.4 0l272.41 272.4c12.54 12.54 12.54 32.86 0.01 45.4l-22.71 22.71A32.124 32.124 0 0 1 905.1 960z"
                    fill="#8a8a8a" p-id="6988"></path>
            </svg>
            <input class="header_div_input" type="text" placeholder="请输入城市(最低市级)">
        </div>
    </div>
    <div class="weatherCondition">
        <table class="weatherTable">
            <tr class="weatherRow">
                <td class="c1 weatherData">城市</td>
                <td class="c2 weatherData city"></td>
            </tr>
            <tr class="weatherRow">
                <td class="c1 weatherData">天气</td>
                <td class="c2 weatherData weather"></td>
            </tr>
            <tr class="weatherRow">
                <td class="c1 weatherData">温度</td>
                <td class="c2 weatherData temperature"></td>
            </tr>
            <tr class="weatherRow">
                <td class="c1 weatherData">风向</td>
                <td class="c2 weatherData winddirection"></td>
            </tr>
            <tr class="weatherRow">
                <td class="c1 weatherData">风力</td>
                <td class="c2 weatherData windpower"></td>
            </tr>
        </table>
    </div>
    <!-- 1、给地图准备一个容器 -->
    <div id="map"></div>
    <script>
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdjaGFvY2hhbyIsImEiOiJjbGU1aDZ2eWUwMXp4M29udmFnNnNyZjBhIn0.2Kd0ZX06ReEdBnZ9XU4XUA';
        // 3、创建地图
        const map = new mapboxgl.Map({
            container: 'map',    //指定地图容器id
            style: 'mapbox://styles/mapbox/streets-v12', //地图风格
            center: [114.30, 30.50],  //地图中心坐标  
            zoom: 5,    //缩放比例
        });

        // 从 localStorage 中获取参数
        let receivedParameter = localStorage.getItem("city");

        // 在这里使用 receivedParameter 进行进一步处理
        let postcode = ''
        let level = ''
        let center = []
        addMask(receivedParameter)

        const input = document.querySelector('.header_div_input')
        input.addEventListener('keyup', function (event) {
            if (event.key === 'Enter') {
                // console.log(input.value);
                addMask(input.value)
            }
            // 将参数存储在 localStorage 中
            localStorage.setItem("currentCity", input.value);
        })

        let oldLayer = Object
        function addMask(text) {
            let postcode = ''
            let level = ''
            let center = []
            // console.log('https://restapi.amap.com/v3/geocode/geo?address=' + result1.text + '&key=0d0f38ca993c65d51f508675413e4a4c')
            fetch('https://restapi.amap.com/v3/geocode/geo?address=' + text + '&key=0d0f38ca993c65d51f508675413e4a4c')
                .then(response => response.json())
                .then(data => {
                    // console.log(data);
                    postcode = data.geocodes[0].adcode
                    level = data.geocodes[0].level
                    center = data.geocodes[0].location.split(',')
                    // console.log(`output->postcode,level,center`, postcode, level, center,typeof center)
                    // console.log('https://geo.datav.aliyun.com/areas_v3/bound/' + postcode + '_full.json');
                    getBound(postcode)
                    addWeatherCondition(postcode)
                    mapTo(center, level)
                });
        }

        function getBound(postcode) {
            fetch('https://geo.datav.aliyun.com/areas_v3/bound/' + postcode + '_full.json')
                .then(res => res.json())
                .then(res => {
                    // console.log(`output->res`, res)
                    if (map.getLayer(`${oldLayer.id}`)) {
                        map.removeLayer(`${oldLayer.id}`)
                    }
                    map.addLayer({
                        id: 'polygon' + postcode,
                        type: 'fill',    //多边形为fill
                        source: {
                            type: 'geojson',
                            data: res
                        },
                        //绘制参数
                        paint: {
                            'fill-color': 'rgb(0,0,255)',
                            'fill-opacity': 0.6,
                        },
                    })
                    oldLayer = map.getLayer('polygon' + postcode)
                    // console.log(oldLayer.id);
                })
        }

        function mapTo(center, level) {
            let zoomIndex = 2
            if (level === '省') {
                zoomIndex = 5.5
            } else if (level === '市') {
                zoomIndex = 7
            }
            map.flyTo({ //飞行到某个点,带飞行动画
                center: center,
                zoom: zoomIndex,
                speed: 0.8,
            })
        }

        function addWeatherCondition(postcode) {
            console.log('https://restapi.amap.com/v3/weather/weatherInfo?city=' + postcode + '&key=0d0f38ca993c65d51f508675413e4a4c')
            fetch('https://restapi.amap.com/v3/weather/weatherInfo?city=' + postcode + '&key=0d0f38ca993c65d51f508675413e4a4c')
                .then(response => response.json())
                .then(weather => {
                    cityWeather = weather.lives[0]
                    $('.city').html(cityWeather.city)
                    $('.header_select').html(cityWeather.city)
                    $('.weather').html(cityWeather.weather)
                    $('.temperature').html(cityWeather.temperature_float + '℃')
                    $('.winddirection').html(cityWeather.winddirection)
                    $('.windpower').html(cityWeather.windpower)
                })
        }


    </script>
</body>

</html>

热门城市跳转页面

draft3.hmtl

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>城市选择</title>
    <script src="lib/jquery.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .container {
            width: 50vw;
            margin: 0 auto;
        }

        .currentCity {
            width: 100%;
            text-align: left;
        }

        .hotCity {
            width: 100%;
            text-align: left;
            display: flex;
            justify-content: space-around;
            align-items: center;
        }

        .city {
            height: 20px;
            line-height: 20px;
            background-color: #ffffff;
            cursor: pointer;
            transition: background-color 0.3s;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            padding: 20px;
        }

        /* 鼠标悬浮时的效果 */
        .city:hover {
            background-color: rgba(193, 193, 193, 0.3);
        }

        /* 按下去的感觉 */
        .city:active {
            background-color: rgb(124, 124, 124);
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="text">当前城市</div>
        <div class="currentCity"></div><hr>
        <div class="text">热门城市</div>
        <div class="hotCity"></div><hr>
    </div>
    <script>
        let cityArr = ['']
        let hotCityArr = ['北京', '上海', '西安', '三亚', '桂林']
        let currentCity = localStorage.getItem('currentCity')
        const div_currentCity = $(`
        <div class="city"
        onclick="goBack('${currentCity}')" 
        >${currentCity}</div>`);
        $('.currentCity').append(div_currentCity)

        hotCityArr.forEach(item => {
            const div_hotCity = $(`
            <div class="city"
            onclick="goBack('${item}')" 
            >${item}</div>`);
            $('.hotCity').append(div_hotCity)
        });

        function goBack(city) {
            location.href = 'http://127.0.0.1:5500/map/drafts2.html'
            // 将参数存储在 localStorage 中
            localStorage.setItem("city", city);
        }
    </script>
</body>

</html>

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值