将两个网页整合为一个完整的网页,包含地图、天气查询、城市搜索功能和天气展示
时间: 2024-12-25 08:29:50 浏览: 9
为了将两个网页整合成一个完整的网页,包含地图、天气查询、城市搜索功能和天气展示,可以参考以下步骤:
### 1. 创建 HTML 结构
首先,创建一个基本的 HTML 文件结构,并引入必要的 CSS 和 JavaScript 文件。
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>综合天气地图</title>
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="reset.css">
</head>
<body>
<div class="container">
<header class="top-box">
<div class="title">
<span class="data">10月28日</span>
<span class="calendar">农历</span>
</div>
<div class="search-box">
<div class="location">
<img src="location-icon.png" alt="位置图标">
<span id="city-name">北京市</span>
</div>
<input type="text" class="search-city" placeholder="请输入城市名称">
<ul class="search-list"></ul>
</div>
</header>
<section class="weather-box">
<div class="tem-box">
<span class="temp">12°C</span>
</div>
<div class="climate-box">
<div class="air">良</div>
<ul class="weather-list">
<li><img src="sun-icon.png" alt="太阳图标">晴</li>
<li><img src="wind-icon.png" alt="风向图标">东北风</li>
<li><img src="humidity-icon.png" alt="湿度图标">53%</li>
</ul>
</div>
</section>
<section class="today-weather">
<div class="range-box">
<span>今日天气:</span>
<span>多云 12°C - 14°C</span>
</div>
<ul class="sun-list">
<li>日出:06:38</li>
<li>日落:17:18</li>
</ul>
</section>
<section class="week-weather-box">
<h2>7天天气预报</h2>
<div class="week-wrap">
<!-- 动态生成的天气预报列表 -->
</div>
</section>
<div id="container" style="height: 500px;"></div>
</div>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=您的高德地图API密钥"></script>
<script src="my-axios.js"></script>
<script src="search.js"></script>
<script src="index.js"></script>
</body>
</html>
```
### 2. 编写 CSS 样式
在 `index.css` 中编写样式,确保页面布局合理且美观。
```css
html, body, #container {
height: 100%;
}
body {
background-image: url('../imgs/R-C.jpg');
margin: 0;
padding: 0;
}
.container {
width: 1200px;
margin: 0 auto;
height: 100%;
}
.top-box {
display: flex;
padding: 50px;
justify-content: space-between;
}
.title .data, .title .calendar {
color: white;
}
.search-box {
display: flex;
justify-content: space-between;
align-items: center;
}
.location {
display: flex;
align-items: center;
margin-right: 20px;
font-size: 16px;
}
.location img {
width: 20px;
height: 20px;
margin-right: 7.1px;
}
.search-city {
width: 234px;
height: 32px;
border: none;
border-radius: 16px;
padding-left: 10px;
background-color: rgba(255, 255, 255, .2);
color: white;
font-size: 14px;
}
.search-list {
width: 100%;
height: 200px;
overflow-y: scroll;
background-color: #fff;
position: absolute;
left: 0;
top: 40px;
display: none;
}
.search-list.show {
display: block;
}
.search-list li {
color: black;
font-size: 14px;
padding: 15px 10px;
}
.search-list li:hover {
background-color: lightgray;
}
.weather-box {
display: flex;
padding-left: 50px;
align-items: flex-end;
}
.weather-box .tem-box {
margin-right: 42px;
display: flex;
flex-direction: column;
}
.weather-box .tem-box .temp span {
font-size: 90px;
display: inline-block;
height: 100px;
margin-bottom: 10px;
line-height: 100px;
}
.climate-box .air {
display: inline;
padding: 5px 10px;
height: 32px;
border-radius: 16px;
background: #65D45A;
text-align: center;
line-height: 32px;
font-family: PingFangSC-Medium;
font-size: 18px;
color: #FFFFFF;
letter-spacing: 0;
font-weight: 500;
}
.climate-box .weather-list {
display: flex;
align-items: flex-end;
margin-top: 21px;
margin-bottom: 28px;
}
.climate-box .weather-list li img {
width: 20px;
height: 20px;
}
.climate-box .weather-list li {
margin-right: 20px;
}
.today-weather {
display: flex;
padding-left: 50px;
}
.today-weather .range-box, .today-weather .range {
font-size: 16px;
}
.today-weather .sun-list {
display: flex;
align-items: center;
margin-left: 50px;
}
.today-weather .sun-list li {
font-size: 16px;
margin-right: 42px;
}
.week-weather-box {
height: 382px;
border-radius: 10px;
background-color: #fff;
width: 1039px;
margin: 30px auto;
padding: 30px;
}
.week-weather-box .title {
font-size: 20px;
color: #3A475A;
letter-spacing: 0;
font-weight: 600;
}
.week-wrap {
display: flex;
margin: 0 auto;
margin-top: 30px;
}
.week-wrap .item {
width: 150px;
height: 254px;
cursor: pointer;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
padding-top: 30px;
}
.week-wrap .item:hover {
background: #F7FAFF;
border-radius: 10px;
}
.week-wrap .item span {
color: #393F48;
}
.week-wrap .item .date-box {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 4px;
}
.week-wrap .item .date-box .dateFormat {
font-size: 16px;
color: #393F48;
letter-spacing: 0;
text-align: center;
font-weight: 400;
}
.week-wrap .item .date-box .date {
font-size: 16px;
color: #88909D;
letter-spacing: 0;
text-align: center;
font-weight: 400;
}
.week-wrap .item img {
width: 46px;
height: 46px;
margin: 12px 0 12px;
}
.week-wrap .item .weather {
font-size: 16px;
color: #393F48;
letter-spacing: 0;
text-align: center;
font-weight: 400;
margin-bottom: 4px;
}
.week-wrap .item .temp {
font-size: 14px;
color: #393F48;
letter-spacing: 0;
text-align: center;
font-weight: 500;
margin-bottom: 10px;
}
.week-wrap .item .wind span {
font-size: 14px;
color: #676C74;
letter-spacing: 0;
text-align: center;
font-weight: 400;
}
```
### 3. 编写 JavaScript 逻辑
在 `index.js` 中编写获取天气数据和地图显示的逻辑。
```javascript
var map = new AMap.Map('container', {
resizeEnable: true,
center: [116.486409, 39.921489],
zoom: 12
});
document.getElementById('adcode').innerHTML = '当前城市adcode:' + map.getAdcode();
AMap.plugin('AMap.Weather', function() {
var weather = new AMap.Weather();
weather.getLive('北京市', function(err, data) {
if (!err) {
var str = [];
str.push('实时天气:<br>');
str.push('城市/区:' + data.city + '<br>');
str.push('天气:' + data.weather + '<br>');
str.push('温度:' + data.temperature + '°<br>');
str.push('风向:' + data.windDirection + '<br>');
str.push('风力:' + data.windPower + '级<br>');
str.push('空气湿度:' + data.humidity + '<br>');
str.push('发布时间:' + data.reportTime + '<br>');
var marker = new AMap.Marker({
map: map,
position: map.getCenter()
});
var infoWin = new AMap.InfoWindow({
content: str.join(''),
isCustom: true,
offset: new AMap.Pixel(0, -37)
});
infoWin.open(map, marker.getPosition());
marker.on('mouseover', function() {
infoWin.open(map, marker.getPosition());
});
}
});
weather.getForecast('北京市', function(err, data) {
if (err) { return; }
var str = [];
for (var i = 0, dayWeather; i < data.forecasts.length; i++) {
dayWeather = data.forecasts[i];
str.push(dayWeather.date + ' ' + dayWeather.dayWeather + ' ' + dayWeather.nightTemp + '~' + dayWeather.dayTemp + '°');
}
document.getElementById('forecast').innerHTML = str.join('<br>');
});
});
function getWeather(cityCode) {
myAxios({
url: 'http://hmajax.itheima.net/api/weather',
params: {
city: cityCode
}
}).then(result => {
const wObj = result.data;
document.querySelector('.title .data').textContent = `${wObj.date} 农历 ${wObj.dateLunar}`;
document.querySelector('.area').textContent = wObj.area;
document.querySelector('.weather-box .temp').textContent = `${wObj.temperature}°`;
document.querySelector('.air').textContent = wObj.psPm25Level;
document.querySelector('.weather-box .weather-list li:nth-child(1)').textContent = `晴 ${wObj.weather}`;
document.querySelector('.weather-box .weather-list li:nth-child(2)').textContent = `风向 ${wObj.windDirection}`;
document.querySelector('.weather-box .weather-list li:nth-child(3)').textContent = `湿度 ${wObj.humidity}%`;
const twObj = wObj.todayWeather;
document.querySelector('.today-weather .range-box span:last-child').textContent = `${twObj.weather} ${twObj.temNight}° - ${twObj.temDay}°`;
document.querySelector('.today-weather .sun-list li:first-child').textContent = `日出 ${twObj.sunriseTime}`;
document.querySelector('.today-weather .sun-list li:last-child').textContent = `日落 ${twObj.sunsetTime}`;
const dayForecastStr = wObj.dayForecast.map(item => `
<div class="item">
<div class="date-box">
<span class="dateFormat">${item.dateFormat}</span>
<span class="date">${item.date}</span>
</div>
<img src="${item.iconUrl}" alt="${item.weather}">
<span class="weather">${item.weather}</span>
<span class="temp">${item.temNight}° - ${item.temDay}°</span>
<span class="wind">${item.windDirection} ${item.windPower}</span>
</div>
`).join('');
document.querySelector('.week-wrap').innerHTML = dayForecastStr;
});
}
getWeather('110100');
document.querySelector('.search-city').addEventListener('input', (e) => {
myAxios({
url: 'http://hmajax.itheima.net/api/weather/city',
params: {
city: e.target.value
}
}).then(result => {
const liStr = result.data.map(item => `<li class="city-item" data-code="${item.adcode}">${item.name}</li>`).join('');
document.querySelector('.search-list').innerHTML = liStr;
});
});
document.querySelector('.search-list').addEventListener('click', e => {
if (e.target.classList.contains('city-item')) {
const cityCode = e.target.dataset.code;
getWeather(cityCode);
}
});
```
### 4. 处理输入框事件
在 `search.js` 中处理输入框的焦点和失焦事件,控制搜索列表的显示和隐藏。
```javascript
const searchList = document.querySelector('.search-list');
document.querySelector('.search-city').addEventListener('input', e => {
if (e.target.value.length > 0) {
searchList.classList.add('show');
} else {
searchList.classList.remove('show');
}
});
document.querySelector('.search-city').addEventListener('blur', e => {
setTimeout(() => {
searchList.classList.remove('show');
}, 500);
});
document.querySelector('.search-city').addEventListener('focus', e => {
if (e.target.value.length > 0) {
searchList.classList.add('show');
}
});
```
### 5. 封装 AJAX 请求
在 `my-axios.js` 中封装 AJAX 请求,简化 HTTP 操作。
```javascript
function myAxios(config) {
return new Promise((resolve, rejected) => {
const xhr = new XMLHttpRequest();
if (config.params) {
const paramsObj = new URLSearchParams(config.params);
const queryString = paramsObj.toString();
config.url += `?${queryString}`;
}
xhr.open(config.method || 'GET', config.url);
xhr.addEventListener('loadend', () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(JSON.parse(xhr.response));
} else {
rejected(new Error(xhr.response));
}
});
if (config.data) {
const jsonStr = JSON.stringify(config.data);
xhr.
阅读全文