我有一张带有两个标记的地图,一个距离很远,为了不缩放太远,我需要为全尺寸的地图制作一个页面,我想在地图之间动态更改,这样用户就不必切换到一个额外的窗口以查看其他位置。这是我想如何做到这一点的图像。我非常感谢所有人的帮助和经验。
这是js代码。
(function() {
window.onload = function() {
// Creating a new map
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(10.2000, -84.4000),
disableDefaultUI: false,
scrollwheel: false,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Creating the JSON data
var json = [
{
"title": "map 1",
"lat": 9.93379260367826,
"lng": -84.22572178326419,
"description": "<br><br><strong>map 1</strong>Description<br><br>"
},
{
"title": "map 2",
"lat": 10.575049399803566,
"lng": -85.58282425643927,
"description": "<br><br><strong>map 2</strong>Description<br><br>"
},
]
// Creating a global infoWindow object that will be reused by all markers
var infoWindow = new google.maps.InfoWindow();
// Looping through the JSON data
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.lat, data.lng);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.title,
icon: 'style/images/mapMarker.png'
});
// Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
(function(marker, data) {
// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
})();