如何在同一页面上添加三个谷歌地图实例
How to add three instances of Google Maps on same page
我在谷歌地图的两个实例中使用Miguel Marnoto的Codepen中的Javascript取得了很好的结果,现在我正试图扩展到三到四个实例,我试图通过命名下面的另外两个实例MapRIght和MapThree来初始化。我运气不好,有人能建议如何扩展Miguel在原始Javascript CodePen中写的内容吗。
https://codepen.io/Marnoto/pen/VLjVZZ
我的新版本在下面。
// *
// * Two maps on the same page, expanded to three
// * 2015 - en.marnoto.com
// *
// necessary variables
var mapLeft, mapRight, mapThree;
var infoWindowLeft, infoWindowRight, infoWindowThree;
// markersData variable stores the information necessary to each marker
var markersDataLeft = [
{
lat: 40.6486333,
lng: -8.745,
name: "Camping Praia do Farol",
address1:"Rua Diogo Cão, 125",
address2: "Praia da Barra",
postalCode: "3830-772 Gafanha da Nazaré" // don't insert comma in the last item of each marker
},
{
lat: 40.54955,
lng: -8.7498167,
name: "Camping Costa Velha",
address1:"Quinta dos Patos, n.º 2",
address2: "Praia da Costa Nova",
postalCode: "3830-453 Gafanha da Encarnação" // don't insert comma in the last item of each marker
},
{
lat: 40.6447167,
lng: -8.7129167,
name: "Camping Gafanha da Boavista",
address1:"Rua dos Balneários do Complexo Desportivo",
address2: "Gafanha da Nazaré",
postalCode: "3830-225 Gafanha da Nazaré" // don't insert comma in the last item of each marker
} // don't insert comma in the last item
];
var markersDataRight = [
{
lat: 40.6386333,
lng: -8.745,
name: "Camping Praia da Barra",
address1:"Rua Diogo Cão, 125",
address2: "Praia da Barra",
postalCode: "3830-772 Gafanha da Nazaré" // don't insert comma in the last item of each marker
},
{
lat: 40.59955,
lng: -8.7498167,
name: "Camping Costa Nova",
address1:"Quinta dos Patos, n.º 2",
address2: "Praia da Costa Nova",
postalCode: "3830-453 Gafanha da Encarnação" // don't insert comma in the last item of each marker
},
{
lat: 40.6247167,
lng: -8.7129167,
name: "Camping Gafanha da Nazaré",
address1:"Rua dos Balneários do Complexo Desportivo",
address2: "Gafanha da Nazaré",
postalCode: "3830-225 Gafanha da Nazaré" // don't insert comma in the last item of each marker
} // don't insert comma in the last item
];
var markersDataThree = [
{
lat: 40.6386333,
lng: -8.745,
name: "Camping Praia da Barra",
address1:"Rua Diogo Cão, 125",
address2: "Praia da Barra",
postalCode: "3830-772 Gafanha da Nazaré" // don't insert comma in the last item of each marker
},
{
lat: 40.59955,
lng: -8.7498167,
name: "Camping Costa Nova",
address1:"Quinta dos Patos, n.º 2",
address2: "Praia da Costa Nova",
postalCode: "3830-453 Gafanha da Encarnação" // don't insert comma in the last item of each marker
},
{
lat: 40.6247167,
lng: -8.7129167,
name: "Camping Gafanha da Nazaré",
address1:"Rua dos Balneários do Complexo Desportivo",
address2: "Gafanha da Nazaré",
postalCode: "3830-225 Gafanha da Nazaré" // don't insert comma in the last item of each marker
} // don't insert comma in the last item
];
function initialize(setMap) {
var mapOptions;
if(setMap == "mapLeft") {
mapOptions = {
center: new google.maps.LatLng(40.601203,-8.668173),
zoom: 11,
mapTypeId: 'roadmap',
};
mapLeft = new google.maps.Map(document.getElementById('map-canvas- left'), mapOptions);
// a new Info Window is created
infoWindowLeft = new google.maps.InfoWindow();
// Event that closes the Info Window with a click on the map
google.maps.event.addListener(mapLeft, 'click', function() {
infoWindowLeft.close();
});
} else {
mapOptions = {
center: new google.maps.LatLng(40.601203,-8.668173),
zoom: 9,
mapTypeId: 'roadmap',
};
mapRight = new google.maps.Map(document.getElementById('map-canvas- right'), mapOptions);
// a new Info Window is created
infoWindowRight = new google.maps.InfoWindow();
// Event that closes the Info Window with a click on the map
google.maps.event.addListener(mapRight, 'click', function() {
infoWindowRight.close();
});
} else {
mapOptions = {
center: new google.maps.LatLng(40.601203,-8.668173),
zoom: 7,
mapTypeId: 'roadmap',
};
mapThree = new google.maps.Map(document.getElementById('map-canvas- three'), mapOptions);
// a new Info Window is created
infoWindowThree = new google.maps.InfoWindow();
// Event that closes the Info Window with a click on the map
google.maps.event.addListener(mapThree, 'click', function() {
infoWindowThree.close();
});
}
// Finally displayMarkers() function is called to begin the markers creation
displayMarkers(setMap);
// Create second map only when initialize function is called for the first time.
// Second time setMap is equal to mapRight, so this condition returns false and it will not run
if(setMap == "mapLeft"){
initialize("mapRight", "mapThree");
}
}
google.maps.event.addDomListener(window, 'load', function(){ initialize("mapLeft") });
// This function will iterate over markersData array
// creating markers with createMarker function
function displayMarkers(setMap){
var markersData;
var map;
if(setMap == "mapLeft"){
markersData = markersDataLeft;
map = mapLeft;
} else {
markersData = markersDataRight;
map = mapRight;
} else {
markersData = markersDataThree;
map = mapThree;
}
// this variable sets the map bounds according to markers position
var bounds = new google.maps.LatLngBounds();
// for loop traverses markersData array calling createMarker function for each marker
for (var i = 0; i < markersData.length; i++){
var latlng = new google.maps.LatLng(markersData[i].lat, markersData[i].lng);
var name = markersData[i].name;
var address1 = markersData[i].address1;
var address2 = markersData[i].address2;
var postalCode = markersData[i].postalCode;
createMarker(setMap, latlng, name, address1, address2, postalCode);
// marker position is added to bounds variable
bounds.extend(latlng);
}
// Finally the bounds variable is used to set the map bounds
// with fitBounds() function
map.fitBounds(bounds);
}
// This function creates each marker and it sets their Info Window content
function createMarker(setMap, latlng, name, address1, address2, postalCode){
var map;
var infoWindow;
if(setMap == "mapLeft"){
map = mapLeft;
infoWindow = infoWindowLeft;
} else {
map = mapRight;
infoWindow = infoWindowRight;
} else {
map = mapThree;
infoWindow = infoWindowThree;
}
var marker = new google.maps.Marker({
map: map,
position: latlng,
title: name
});
// This event expects a click on a marker
// When this event is fired the Info Window content is created
// and the Info Window is opened.
google.maps.event.addListener(marker, 'click', function() {
// Creating the content to be inserted in the infowindow
var iwContent = '<div id="iw_container">' +
'<div class="iw_title">' + name + '</div>' +
'<div class="iw_content">' + address1 + '<br />' +
address2 + '<br />' +
postalCode + '</div></div>';
// including content to the Info Window.
infoWindow.setContent(iwContent);
// opening the Info Window in the current map and at the current marker location.
infoWindow.open(map, marker);
});
}
您的代码有语法错误1.在一个if ... else if ... else ...
语句中只能有一个else
。2.传递到document.getElementById()中的字符串必须与实际的DOM元素id匹配,您的字符串中有额外的空格。
概念验证小提琴
代码片段:
// *
// * Two maps on the same page, expanded to three
// * 2015 - en.marnoto.com
// *
// necessary variables
var mapLeft, mapRight, mapThree;
var infoWindowLeft, infoWindowRight, infoWindowThree;
// markersData variable stores the information necessary to each marker
var markersDataLeft = [{
lat: 40.6486333,
lng: -8.745,
name: "Camping Praia do Farol",
address1: "Rua Diogo Cão, 125",
address2: "Praia da Barra",
postalCode: "3830-772 Gafanha da Nazaré" // don't insert comma in the last item of each marker
}, {
lat: 40.54955,
lng: -8.7498167,
name: "Camping Costa Velha",
address1: "Quinta dos Patos, n.º 2",
address2: "Praia da Costa Nova",
postalCode: "3830-453 Gafanha da Encarnação" // don't insert comma in the last item of each marker
}, {
lat: 40.6447167,
lng: -8.7129167,
name: "Camping Gafanha da Boavista",
address1: "Rua dos Balneários do Complexo Desportivo",
address2: "Gafanha da Nazaré",
postalCode: "3830-225 Gafanha da Nazaré" // don't insert comma in the last item of each marker
} // don't insert comma in the last item
];
var markersDataRight = [{
lat: 40.6386333,
lng: -8.745,
name: "Camping Praia da Barra",
address1: "Rua Diogo Cão, 125",
address2: "Praia da Barra",
postalCode: "3830-772 Gafanha da Nazaré" // don't insert comma in the last item of each marker
}, {
lat: 40.59955,
lng: -8.7498167,
name: "Camping Costa Nova",
address1: "Quinta dos Patos, n.º 2",
address2: "Praia da Costa Nova",
postalCode: "3830-453 Gafanha da Encarnação" // don't insert comma in the last item of each marker
}, {
lat: 40.6247167,
lng: -8.7129167,
name: "Camping Gafanha da Nazaré",
address1: "Rua dos Balneários do Complexo Desportivo",
address2: "Gafanha da Nazaré",
postalCode: "3830-225 Gafanha da Nazaré" // don't insert comma in the last item of each marker
} // don't insert comma in the last item
];
var markersDataThree = [{
lat: 40.6386333,
lng: -8.745,
name: "Camping Praia da Barra",
address1: "Rua Diogo Cão, 125",
address2: "Praia da Barra",
postalCode: "3830-772 Gafanha da Nazaré" // don't insert comma in the last item of each marker
}, {
lat: 40.59955,
lng: -8.7498167,
name: "Camping Costa Nova",
address1: "Quinta dos Patos, n.º 2",
address2: "Praia da Costa Nova",
postalCode: "3830-453 Gafanha da Encarnação" // don't insert comma in the last item of each marker
}, {
lat: 40.6247167,
lng: -8.7129167,
name: "Camping Gafanha da Nazaré",
address1: "Rua dos Balneários do Complexo Desportivo",
address2: "Gafanha da Nazaré",
postalCode: "3830-225 Gafanha da Nazaré" // don't insert comma in the last item of each marker
} // don't insert comma in the last item
];
function initialize(setMap) {
var mapOptions;
if (setMap == "mapLeft") {
mapOptions = {
center: new google.maps.LatLng(40.601203, -8.668173),
zoom: 11,
mapTypeId: 'roadmap',
};
mapLeft = new google.maps.Map(document.getElementById('map-canvas-left'), mapOptions);
// a new Info Window is created
infoWindowLeft = new google.maps.InfoWindow();
// Event that closes the Info Window with a click on the map
google.maps.event.addListener(mapLeft, 'click', function() {
infoWindowLeft.close();
});
} else if (setMap == "mapRight") {
mapOptions = {
center: new google.maps.LatLng(40.601203, -8.668173),
zoom: 9,
mapTypeId: 'roadmap',
};
mapRight = new google.maps.Map(document.getElementById('map-canvas-right'), mapOptions);
// a new Info Window is created
infoWindowRight = new google.maps.InfoWindow();
// Event that closes the Info Window with a click on the map
google.maps.event.addListener(mapRight, 'click', function() {
infoWindowRight.close();
});
} else if (setMap == "mapThree") {
mapOptions = {
center: new google.maps.LatLng(40.601203, -8.668173),
zoom: 7,
mapTypeId: 'roadmap',
};
mapThree = new google.maps.Map(document.getElementById('map-canvas-three'), mapOptions);
// a new Info Window is created
infoWindowThree = new google.maps.InfoWindow();
// Event that closes the Info Window with a click on the map
google.maps.event.addListener(mapThree, 'click', function() {
infoWindowThree.close();
});
}
// Finally displayMarkers() function is called to begin the markers creation
displayMarkers(setMap);
// Create second map only when initialize function is called for the first time.
// Second time setMap is equal to mapRight, so this condition returns false and it will not run
if (setMap == "mapLeft") {
initialize("mapRight");
} else if (setMap == "mapRight") {
initialize("mapThree");
}
}
google.maps.event.addDomListener(window, 'load', function() {
initialize("mapLeft")
});
// This function will iterate over markersData array
// creating markers with createMarker function
function displayMarkers(setMap) {
var markersData;
var map;
if (setMap == "mapLeft") {
markersData = markersDataLeft;
map = mapLeft;
} else if (setMap == "mapRight") {
markersData = markersDataRight;
map = mapRight;
} else {
markersData = markersDataThree;
map = mapThree;
}
// this variable sets the map bounds according to markers position
var bounds = new google.maps.LatLngBounds();
// for loop traverses markersData array calling createMarker function for each marker
for (var i = 0; i < markersData.length; i++) {
var latlng = new google.maps.LatLng(markersData[i].lat, markersData[i].lng);
var name = markersData[i].name;
var address1 = markersData[i].address1;
var address2 = markersData[i].address2;
var postalCode = markersData[i].postalCode;
createMarker(setMap, latlng, name, address1, address2, postalCode);
// marker position is added to bounds variable
bounds.extend(latlng);
}
// Finally the bounds variable is used to set the map bounds
// with fitBounds() function
map.fitBounds(bounds);
}
// This function creates each marker and it sets their Info Window content
function createMarker(setMap, latlng, name, address1, address2, postalCode) {
var map;
var infoWindow;
if (setMap == "mapLeft") {
map = mapLeft;
infoWindow = infoWindowLeft;
} else if (setMap == "mapRight") {
map = mapRight;
infoWindow = infoWindowRight;
} else {
map = mapThree;
infoWindow = infoWindowThree;
}
var marker = new google.maps.Marker({
map: map,
position: latlng,
title: name
});
// This event expects a click on a marker
// When this event is fired the Info Window content is created
// and the Info Window is opened.
google.maps.event.addListener(marker, 'click', function() {
// Creating the content to be inserted in the infowindow
var iwContent = '<div id="iw_container">' +
'<div class="iw_title">' + name + '</div>' +
'<div class="iw_content">' + address1 + '<br />' +
address2 + '<br />' +
postalCode + '</div></div>';
// including content to the Info Window.
infoWindow.setContent(iwContent);
// opening the Info Window in the current map and at the current marker location.
infoWindow.open(map, marker);
});
}
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
padding: 0;
}
.container {
width: 100%;
display: flex;
}
#map-canvas-left,
#map-canvas-right,
#map-canvas-three {
height: 250px;
width: 550px;
}
#iw_container .iw_title {
font-size: 16px;
font-weight: bold;
}
.iw_content {
padding: 15px 15px 15px 0;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<div class="container">
<div id="map-canvas-left"></div>
<div id="map-canvas-right"></div>
<div id="map-canvas-three"></div>
</div>
相关文章:
- KnockoutJS-组件-多个实例
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 如何使用PHP和JS级联三个下拉列表
- 灯光问题(使用三个.js)
- 在三个.js中创建球体(例如地球)的磁力线
- 如何获取单选按钮的多个实例的选定单选按钮值
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- 根据前两个下拉ID显示第三个下拉列表
- 在数组JS中查找三个重复项
- Meteor和S3:如何通过一次上传/编辑定义三个不同的图像
- 如何通过osascript处理同一应用程序的两个实例
- 如何替换javascript字符串中的前三个连字符
- 设置三个express实例:admin、site和app
- 按第三个实例拆分字符串
- innerHTML的三个实例正在工作,第四个没有
- 如何在同一页面上添加三个谷歌地图实例
- 将一个数组的三个实例推送到另一个数组——javaScript