- 创建一个空的边界对象(一个google.maps.LatLngBounds)
- 将所有标记添加到它
- 用它来居中和缩放你的地图
像这样的东西:
var markersArray = [];
// create an empty bounds object
var bounds = new google.maps.LatLngBounds();
downloadUrl("eventsxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("id");
var address = markers[i].getAttribute("id");
var type = markers[i].getAttribute("venue_type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
// add each marker's location to the bounds
bounds.extend(point);
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
markersArray.push(marker);
bindInfoWindow(marker, map, infoWindow, html);
}
// center and zoom the map to fit the bounds
map.fitBounds(bounds);
});
要平移到该边界的中心,而不是 fitBounds,请使用:
map.panTo(bounds.getCenter());
请注意,这不会更改地图的缩放级别。
概念证明小提琴
代码片段:
// The following example creates complex markers to indicate beaches near
// Sydney, NSW, Australia.
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 10,
center: {
lat: -33.9,
lng: 151.2
},
});
const infoWindow = new google.maps.InfoWindow();
var markersArray = [];
// create an empty bounds object
var bounds = new google.maps.LatLngBounds();
// downloadUrl("eventsxml.php", function(data) {
// var xml = data.responseXML;
var xml = parseXml(xmlString);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("id");
var address = markers[i].getAttribute("id");
var type = markers[i].getAttribute("venue_type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
// add each marker's location to the bounds
bounds.extend(point);
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
title: name
});
markersArray.push(marker);
bindInfoWindow(marker, map, infoWindow, html);
}
// center and zoom the map to fit the bounds
map.fitBounds(bounds);
// });
}
// Data for the markers consisting of a name, a LatLng and a venue_type.
const xmlString = '<markers><marker id="Bondi Beach" lat="-33.890542" lng="151.274856" venue_type="beach"/><marker id="Coogee Beach" lat="-33.923036" lng="151.259052" venue_type="beach"/><marker id="Cronulla Beach" lat="-34.028249" lng="151.157507" venue_type="beach"/><marker id="Manly Beach" lat="-33.80010128657071" lng="151.28747820854187" venue_type="beach"/><marker id="Maroubra Beach" lat="-33.950198" lng="151.259302" venue_type="beach"/><marker id="Sydney" lat="-33.8688197" lng="151.2092955" venue_type="town"/><marker id="Wagga Wagga" lat="-35.1081689" lng="147.3598323" venue_type="town"/></markers>';
var customIcons = {
beach: {
icon: "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png",
},
town: {
icon: "http://maps.google.com/mapfiles/ms/micons/blue.png",
}
};
function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('MicrosoftXMLDOM');
doc.loadXML(str);
return doc;
} else if (window.DOMParser) {
return (new DOMParser).parseFromString(str, 'text/xml');
}
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Complex Marker Icons</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=&v=weekly" defer></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
</body>
</html>