• WebGIS之实现查询地区天气并让地区高亮


    一.预览>>


     

    二.思路>>


            根据搜索框的内容来进行页面视角的切换,对应的地区高亮,右边有关天气的地方实时更新,并且因为代码体量非常小,并没有选择在框架下完成。直接一个html文件搞定了,但实际上还是有一些坑的,比如不太了解的人会把key引入错,关于请求高德接口方面,注意阅读看是否需要其它参数,地理/逆地理编码-基础 API 文档-开发指南-Web服务 API|高德地图API (amap.com)icon-default.png?t=N7T8https://lbs.amap.com/api/webservice/guide/api/georegeo可能之后时间过得久了,对应API发生了变化,及时查阅文档。

    三.代码>> 


    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Weathertitle>
    7. <link href="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.css" rel="stylesheet">
    8. <script src="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.js">script>
    9. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js">script>
    10. <style>
    11. body {
    12. margin: 0;
    13. padding: 0;
    14. }
    15. #header {
    16. width: 100%;
    17. height: 100px;
    18. background-color: #212121;
    19. overflow: hidden;
    20. /* 解决margin塌陷,因为group设置了margin-top*/
    21. }
    22. #map {
    23. width: 100%;
    24. position: absolute;
    25. top: 100px;
    26. bottom: 0;
    27. }
    28. #map .information {
    29. padding: 0;
    30. margin: 0;
    31. list-style: none;
    32. width: 200px;
    33. height: 300px;
    34. position: absolute;
    35. z-index: 1;
    36. top: 10px;
    37. right: 10px;
    38. color: #fff;
    39. background-color: rgba(0, 0, 0, 0.4);
    40. border-radius: 5px;
    41. padding: 10px;
    42. display: flex;
    43. align-items: stretch;
    44. justify-content: space-around;
    45. flex-direction: column;
    46. }
    47. #map .information li {
    48. border-bottom: 1px solid aqua;
    49. font-style: 20px;
    50. }
    51. .group {
    52. display: flex;
    53. line-height: 28px;
    54. align-items: center;
    55. position: relative;
    56. max-width: 190px;
    57. margin: 0 auto;
    58. margin-top: 20px;
    59. }
    60. .input {
    61. width: 100%;
    62. height: 40px;
    63. line-height: 28px;
    64. padding: 0 1rem;
    65. padding-left: 2.5rem;
    66. border: 2px solid transparent;
    67. border-radius: 8px;
    68. outline: none;
    69. background-color: #f3f3f4;
    70. color: #0d0c22;
    71. transition: .3s ease;
    72. }
    73. .input::placeholder {
    74. color: #9e9ea7;
    75. }
    76. .input:focus,
    77. input:hover {
    78. outline: none;
    79. border-color: rgba(234, 76, 137, 0.4);
    80. background-color: #fff;
    81. box-shadow: 0 0 0 4px rgb(234 76 137 / 10%);
    82. }
    83. .icon {
    84. position: absolute;
    85. left: 1rem;
    86. fill: #9e9ea7;
    87. width: 1rem;
    88. height: 1rem;
    89. }
    90. style>
    91. head>
    92. <body>
    93. <div id="header">
    94. <div class="group">
    95. <svg class="icon" aria-hidden="true" viewBox="0 0 24 24">
    96. <g>
    97. <path
    98. d="M21.53 20.47l-3.66-3.66C19.195 15.24 20 13.214 20 11c0-4.97-4.03-9-9-9s-9 4.03-9 9 4.03 9 9 9c2.215 0 4.24-.804 5.808-2.13l3.66 3.66c.147.146.34.22.53.22s.385-.073.53-.22c.295-.293.295-.767.002-1.06zM3.5 11c0-4.135 3.365-7.5 7.5-7.5s7.5 3.365 7.5 7.5-3.365 7.5-7.5 7.5-7.5-3.365-7.5-7.5z">
    99. path>
    100. g>
    101. svg>
    102. <input placeholder="输入城市名称" type="search" class="input">
    103. div>
    104. div>
    105. <div id="map">
    106. <ul class="information">
    107. <li>当前时间li>
    108. <li>城市li>
    109. <li>天气li>
    110. <li>温度li>
    111. <li>风向li>
    112. <li>风力li>
    113. ul>
    114. div>
    115. <script type="text/javascript">
    116. window._AMapSecurityConfig = {
    117. securityJsCode: "写你自己的",
    118. };
    119. script>
    120. <script type="text/javascript"
    121. src="https://webapi.amap.com/maps?v=2.0&key=你自己的web端key"> script>
    122. <script>
    123. const input = document.querySelector('.input');
    124. mapboxgl.accessToken = 'pk.eyJ1IjoiY3VkODUiLCJhIjoiY2xrYnFncXZhMGc1cTNlbmFrNHN1N2cxeCJ9.69E3f8nMJkvqQDRhLSojVw';
    125. const map = new mapboxgl.Map({
    126. container: 'map',
    127. style: 'mapbox://styles/mapbox/streets-v12',
    128. zoom: 10,
    129. center: [116.41667, 39.91667]
    130. });
    131. document.addEventListener('keypress', function (e) {
    132. if (e.key === 'Enter') {
    133. const cityName = input.value;
    134. //使用完清空,便于下次使用
    135. input.value = ''
    136. queryWeather(cityName)
    137. }
    138. })
    139. function queryWeather(cityName) {
    140. //加载天气查询插件,因为只展示当前天气,没必要调用接口
    141. AMap.plugin("AMap.Weather", function () {
    142. //创建天气查询实例
    143. var weather = new AMap.Weather();
    144. //执行实时天气信息查询
    145. weather.getLive(cityName, function (err, data) {
    146. console.log(data);
    147. if (!err) {
    148. updateWeatherInfo(data)
    149. updateMap(data.city, data.adcode)
    150. } else {
    151. alert(err)
    152. }
    153. });
    154. });
    155. }
    156. function updateWeatherInfo(data) {
    157. const timeElement = document.querySelector('.information li:nth-child(1)')
    158. const cityElement = document.querySelector('.information li:nth-child(2)');
    159. const weatherElement = document.querySelector('.information li:nth-child(3)');
    160. const tempElement = document.querySelector('.information li:nth-child(4)');
    161. const windDirectionElement = document.querySelector('.information li:nth-child(5)');
    162. const windPowerElement = document.querySelector('.information li:nth-child(6)');
    163. timeElement.textContent = '当前时间:' + data.reportTime;
    164. cityElement.textContent = '城市:' + data.city;
    165. weatherElement.textContent = '天气:' + data.weather;
    166. tempElement.textContent = '温度:' + data.temperature + '°C';
    167. windDirectionElement.textContent = '风向:' + data.windDirection;
    168. windPowerElement.textContent = '风力:' + data.windPower + '级';
    169. }
    170. function updateMap(city, adcode) {
    171. //调用高德的地理编码API
    172. //记住是这个key绑定的是Web服务,跟前面的key不是一种
    173. axios({
    174. url: `https://restapi.amap.com/v3/geocode/geo?address=${city}&adcode=${adcode}&key=填自己的key`
    175. }).then(result => {
    176. // console.log(typeof (result.data.geocodes[0].location)) //String类型
    177. var locationString = result.data.geocodes[0].location;
    178. var coordinates = locationString.split(","); // 经度和纬度之间用逗号分隔,可以根据实际情况调整分隔符
    179. // 创建一个新的 LngLat 对象
    180. var newCenter = new mapboxgl.LngLat(parseFloat(coordinates[0]), parseFloat(coordinates[1]));
    181. //实现视角跳转
    182. map.flyTo({
    183. center: newCenter,
    184. //使动画平滑
    185. essential: true
    186. })
    187. }).catch(error => {
    188. console.log(error.message);
    189. })
    190. // 先检查是否已存在同名的数据源和图层,如果存在,则移除
    191. if (map.getSource('json')) {
    192. map.removeLayer('place'); // 移除图层
    193. map.removeSource('json'); // 移除数据源
    194. }
    195. //省市的url带有_full,观察adcode的区别来确定模板字符串
    196. let url = ''
    197. if (adcode[adcode.length - 1] == 0) {
    198. url = `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}_full.json`
    199. } else {
    200. url = `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}.json`
    201. }
    202. // console.log(url);
    203. map.addSource('json', {
    204. type: 'geojson',
    205. data: url
    206. })
    207. map.addLayer({
    208. id: 'place',
    209. type: 'fill',
    210. source: 'json',
    211. paint: {
    212. 'fill-color': '#ff5733', // 设置填充颜色为橙红色
    213. 'fill-opacity': 0.5, // 设置填充不透明度
    214. 'fill-outline-color': '#ffffff', // 设置边界线的颜色为白色
    215. 'fill-outline-width': 2 // 设置边界线的宽度
    216. }
    217. })
    218. }
    219. script>
    220. body>
    221. html>

             

  • 相关阅读:
    Java入门基础笔记
    Dropout回顾
    多测师肖sir_第二个月第一讲html001
    js逆向之反调试之无限debugger解决
    基于SSM的校园快递一站式服务系统设计与实现
    C/C++ 字符串问题总结
    HTML表格与表单
    Item 42: Consider emplacement instead of insertion.
    查看当前所有的数据库
    C++中如何描述table表格类型数据结构
  • 原文地址:https://blog.csdn.net/weixin_73810008/article/details/136766673