如何合并这两个HTML代码?
我有一个代码显示海拔高度,另一个代码显示天气信息。有没有办法将这两个代码合并在一起,这样我就有了一个同时具有这两个特征的地图?我正在用记事本做这件事 代码1:如何合并这两个HTML代码?,html,google-maps,merge,Html,Google Maps,Merge,我有一个代码显示海拔高度,另一个代码显示天气信息。有没有办法将这两个代码合并在一起,这样我就有了一个同时具有这两个特征的地图?我正在用记事本做这件事 代码1: <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8">
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Elevation service</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits: google.maps.weather.TemperatureUnit.CELSIUS
});
weatherLayer.setMap(map);
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
}
var elevator;
var map;
var infowindow = new google.maps.InfoWindow();
var denali = new google.maps.LatLng(60.750000, -139.500000);
function initialize() {
var mapOptions = {
zoom: 8,
center: denali,
mapTypeId: 'terrain'
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// Create an ElevationService
elevator = new google.maps.ElevationService();
// Add a listener for the click event and call getElevation on that location
google.maps.event.addListener(map, 'click', getElevation);
}
function getElevation(event) {
var locations = [];
// Retrieve the clicked location and push it on the array
var clickedLocation = event.latLng;
locations.push(clickedLocation);
// Create a LocationElevationRequest object using the array's one value
var positionalRequest = {
'locations': locations
}
// Initiate the location request
elevator.getElevationForLocations(positionalRequest, function(results, status) {
if (status == google.maps.ElevationStatus.OK) {
// Retrieve the first result
if (results[0]) {
// Open an info window indicating the elevation at the clicked position
infowindow.setContent('The elevation at this point <br>is ' + results[0].elevation + ' meters.');
infowindow.setPosition(clickedLocation);
infowindow.open(map);
} else {
alert('No results found');
}
} else {
alert('Elevation service failed due to: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
提升服务
html,正文,#地图画布{
身高:100%;
边际:0px;
填充:0px
}
var weatherLayer=new google.maps.weather.weatherLayer({
温度单位:google.maps.weather.TemperatureUnit.CELSIUS
});
weatherLayer.setMap(地图);
var cloudLayer=new google.maps.weather.cloudLayer();
cloudLayer.setMap(map);
}
无功电梯;
var映射;
var infowindow=new google.maps.infowindow();
var denali=new google.maps.LatLng(60.750000,-139.500000);
函数初始化(){
变量映射选项={
缩放:8,
中心:德纳利,
mapTypeId:'地形'
}
map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
//创建提升服务
电梯=新的google.maps.ElevationService();
//为click事件添加侦听器,并在该位置调用getElevation
google.maps.event.addListener(映射,'click',getElevation);
}
函数getElevation(事件){
var位置=[];
//检索单击的位置并将其推送到阵列上
var clickedLocation=event.latLng;
位置。推送(单击位置);
//使用数组的一个值创建LocationElevationRequest对象
变量位置请求={
“位置”:位置
}
//启动位置请求
电梯。getElevationForLocations(位置请求、功能(结果、状态){
if(status==google.maps.ElevationStatus.OK){
//检索第一个结果
如果(结果[0]){
//打开一个信息窗口,指示单击位置的高程
infowindow.setContent('此时的高程
为'+results[0]。高程为'+meters');
信息窗口。设置位置(单击位置);
打开(地图);
}否则{
警报(“未找到结果”);
}
}否则{
警报('由于:'+状态,提升服务失败);
}
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
代码2:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Weather layer</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=weather"></script>
<script>
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(60.750000, -139.500000),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits: google.maps.weather.TemperatureUnit.CELSIUS
});
weatherLayer.setMap(map);
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
气象层
html,正文,#地图画布{
身高:100%;
边际:0px;
填充:0px
}
函数初始化(){
变量映射选项={
缩放:8,
中心:新google.maps.LatLng(60.750000,-139.500000),
mapTypeId:google.maps.mapTypeId.TERRAIN
};
var map=new google.maps.map(document.getElementById('map-canvas'),
地图选项);
var weatherLayer=new google.maps.weather.weatherLayer({
温度单位:google.maps.weather.TemperatureUnit.CELSIUS
});
weatherLayer.setMap(地图);
var cloudLayer=new google.maps.weather.cloudLayer();
cloudLayer.setMap(map);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
您可能希望将代码放入一个单独的.js文件中,并将其链接到中。这样,代码就彼此相邻,因此您可以同时获得这两种效果,而其他方法则无法实现这两种效果。您已经在链接一个来自google的脚本文件——您只需将您的代码放入一个.js文件中,并以相同的方式链接它