我試圖用谷歌地圖API V3在頁面上放置帶有不同位置的多個標記的2個谷歌地圖。這一步我已經得到了:在一個頁面上具有多個標記的多重Google地圖。
jQuery(function($) {
// Asynchronously Load the map API
var script = document.createElement('script');
script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(script);
});
function initialize() {
var map,map2,mapOptions,mapOptions2,markers,markers2,infoWindowContent,infoWindowContent2;
var bounds = new google.maps.LatLngBounds();
var bounds2 = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: 'roadmap',
scrollwheel: false,
styles:[
{
"stylers": [
{ "saturation": -100 }
]
}
],
};
var mapOptions2 = {
mapTypeId: 'roadmap',
scrollwheel: false,
styles:[
{
"stylers": [
{ "saturation": -100 }
]
}
],
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("google-maps"), mapOptions);
map2 = new google.maps.Map(document.getElementById("locations-google"), mapOptions2);
map.setTilt(45);
map2.setTilt(45);
// Multiple Markers
var markers = [
['TITLE HERE', 1,1],
['TITLE HERE', 1,1]
];
var markers2 = [
['TITLE HERE', 1,1],
['TITLE HERE', 1,1]
];
var infoWindowContent = [
['<div class="info_content">' +
'<h4>TITLE HERE</h4>' +
'info here' +
'</div>'],
['<div class="info_content">' +
'<h4>TITLE HERE</h4>' +
'info here' +
'</div>']
];
var infoWindowContent2 = [
['<div class="info_content">' +
'<h4>TITLE HERE</h4>' +
'info here' +
'</div>'],
['<div class="info_content">' +
'<h4>TITLE HERE</h4>' +
'info here' +
'</div>']
];
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;
var infoWindow2 = new google.maps.InfoWindow(), marker2, i;
// Loop through our array of markers & place each one on the map
for(i = 0; i < markers.length; i++) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0],
icon: "<?php echo get_stylesheet_directory_uri(); ?>/images/marker.png"
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
for(i = 0; i < markers2.length; i++) {
var position2 = new google.maps.LatLng(markers2[i][1], markers2[i][2]);
bounds2.extend(position2);
marker2 = new google.maps.Marker({
position: position2,
map: map2,
title: markers2[i][0],
icon: "<?php echo get_stylesheet_directory_uri(); ?>/images/marker.png"
});
// Allow each marker to have an info window
google.maps.event.addListener(marker2, 'click', (function(marker2, i) {
return function() {
infoWindow.setContent(infoWindowContent2[i][0]);
infoWindow.open(map2, marker2);
}
})(marker2, i));
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds2);
}
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(8);
google.maps.event.removeListener(boundsListener);
});
}
但是當我運行此,第一張地圖將正常工作和顯示位置等正確的號碼但是,當涉及到第二個它不會加載,給我控制檯中出現此錯誤「Uncaught TypeError:無法讀取屬性'lat'爲null」。我非常困惑,爲什麼它這樣做,我根本找不到它。
謝謝!
:
map
第二個呼叫當fitBounds方法被調用時,它們可能會被省略(fitBounds會同時設置,center和zoom) – 2014-09-04 17:05:49
嘿,沒錯。添加中心和縮放級別實際上解決了錯誤,但我沒有意識到fitBounds在同一個地圖對象上被調用了兩次。接得好。 – MrUpsidown 2014-09-05 07:38:35