我正在使用Google Map V3。我正在尝试将缩放级别设置为占用所有标记。代码在
之下 <script>
var Duabi = new google.maps.LatLng(25.232274,55.332642);
var marker;
var map;
var locations = [
['Al Ain Rotana',24.2187077269409,55.7486816157711,"<a href='/ems/public/Company/Manage/view/est_id/MjYxNA==' target='_blank'>View Details<\/a>"],['Al Bustan Rotana',25.2483086562942,55.3453971796322,"<a href='/ems/public/Company/Manage/view/est_id/MTM3Nw==' target='_blank'>View Details<\/a>"],['Al Ghurair Arjaan by Rotana',25.2695672065064,55.3171133437058,"<a href='/ems/public/Company/Manage/view/est_id/MTM5NTkw' target='_blank'>View Details<\/a>"],['Al Ghurair Rayhaan By Rotana',25.26751,55.31715,"<a href='/ems/public/Company/Manage/view/est_id/MTI5Njk2' target='_blank'>View Details<\/a>"],['Al Ghurair Rayhaan By Rotana',25.2689526605375,55.3169359915134,"<a href='/ems/public/Company/Manage/view/est_id/MTI5Njk1' target='_blank'>View Details<\/a>"],['Al Maha Arjaan By Rotana',24.4921202648678,54.3674452492895,"<a href='/ems/public/Company/Manage/view/est_id/NzQwNQ==' target='_blank'>View Details<\/a>"],['Al Murooj Rotana',25.2027734837341,55.2778133352192,"<a href='/ems/public/Company/Manage/view/est_id/MTMyNDA=' target='_blank'>View Details<\/a>"],['Al Rawda Arjaan By Rotana',24.4691980739652,54.3723774450805,"<a href='/ems/public/Company/Manage/view/est_id/MjQzOQ==' target='_blank'>View Details<\/a>"],['Amwaj Rotana Jumeirah Beach Residence',25.0734792101092,55.1308173703686,"<a href='/ems/public/Company/Manage/view/est_id/NjEzNDQ=' target='_blank'>View Details<\/a>"],['Arjaan By Rotana',25.09568,55.15381,"<a href='/ems/public/Company/Manage/view/est_id/NjI0NDc=' target='_blank'>View Details<\/a>"],['Beach Rotana Abu Dhabi',24.4945924700841,54.3841634196444,"<a href='/ems/public/Company/Manage/view/est_id/MjE3MA==' target='_blank'>View Details<\/a>"],['BurJuman Arjaan By Rotana',25.2518237482319,55.3014216739748,"<a href='/ems/public/Company/Manage/view/est_id/MzE2MDc=' target='_blank'>View Details<\/a>"],['Capital Centre Arjaan By Rotana',24.41996,54.44185,"<a href='/ems/public/Company/Manage/view/est_id/MTI1NTkx' target='_blank'>View Details<\/a>"],['Capital Centre Rotana',24.41982,54.44219,"<a href='/ems/public/Company/Manage/view/est_id/NjY2ODI=' target='_blank'>View Details<\/a>"],['Fujairah Rotana Resort & Spa - Al Aqah Beach',25.50868,56.36336,"<a href='/ems/public/Company/Manage/view/est_id/NDMxNA==' target='_blank'>View Details<\/a>"],['Grand Rotana',24.2187077269409,55.7486816157711,"<a href='/ems/public/Company/Manage/view/est_id/MTUxNTk=' target='_blank'>View Details<\/a>"],['Hala Arjaan By Rotana',24.50123,54.37793,"<a href='/ems/public/Company/Manage/view/est_id/NjUwOTU=' target='_blank'>View Details<\/a>"],['Hili Rayhaan By Rotana',24.27384,55.77954,"<a href='/ems/public/Company/Manage/view/est_id/NjYzMjA=' target='_blank'>View Details<\/a>"],['Hili Reyhaan by Rotana',24.27357,55.77900,"<a href='/ems/public/Company/Manage/view/est_id/MTQxNTM1' target='_blank'>View Details<\/a>"],['International Rotana Inn',24.4974871914531,54.373612287925,"<a href='/ems/public/Company/Manage/view/est_id/MjMzMQ==' target='_blank'>View Details<\/a>"],['Jumeira Rotana',25.2373282998585,55.274198112447,"<a href='/ems/public/Company/Manage/view/est_id/MjYwOA==' target='_blank'>View Details<\/a>"],['Khalidiya Palace Rayhaan By Rotana',24.45975,54.31256,"<a href='/ems/public/Company/Manage/view/est_id/NjYzMTU=' target='_blank'>View Details<\/a>"],['Marina Mall Arjaan by Rotana',24.4756970326267,54.3218642298077,"<a href='/ems/public/Company/Manage/view/est_id/NjY2ODk=' target='_blank'>View Details<\/a>"],['Media Rotana',25.0998417925132,55.179632179501,"<a href='/ems/public/Company/Manage/view/est_id/NjEzNDY=' target='_blank'>View Details<\/a>"],['Media Rotana events and promotions',25.0998417925132,55.179632179501,"<a href='/ems/public/Company/Manage/view/est_id/MTI4MTI5' target='_blank'>View Details<\/a>"],['Mina Al Arab Rotana Resort',25.71865,55.84713,"<a href='/ems/public/Company/Manage/view/est_id/NjY2OTc=' target='_blank'>View Details<\/a>"],['Nour Arjaan By Rotana',25.12265,56.32716,"<a href='/ems/public/Company/Manage/view/est_id/MTI3Mjg5' target='_blank'>View Details<\/a>"],['Park Arjaan By Rotana',24.42406,54.47348,"<a href='/ems/public/Company/Manage/view/est_id/NjUwOTQ=' target='_blank'>View Details<\/a>"],['Park Rotana Abu Dhabi',24.42361,54.47384,"<a href='/ems/public/Company/Manage/view/est_id/NjUwOTI=' target='_blank'>View Details<\/a>"],['Rihab Rotana Dubai',25.2538952503536,55.3357809787836,"<a href='/ems/public/Company/Manage/view/est_id/ODI1Ng==' target='_blank'>View Details<\/a>"],['Rimal Rotana',25.2643012871287,55.3268515021216,"<a href='/ems/public/Company/Manage/view/est_id/Nzk5MA==' target='_blank'>View Details<\/a>"],['Rose Rayhaan By Rotana',25.21180624263,55.2765817489046,"<a href='/ems/public/Company/Manage/view/est_id/NjEzNDc=' target='_blank'>View Details<\/a>"],['Rotana Beach Club',24.4945924700841,54.3841634196444,"<a href='/ems/public/Company/Manage/view/est_id/Nzg3MA==' target='_blank'>View Details<\/a>"],['Rotana Jet',24.42769,54.45760,"<a href='/ems/public/Company/Manage/view/est_id/MTM3ODcy' target='_blank'>View Details<\/a>"],['Rotana Jet',24.42769,54.45760,"<a href='/ems/public/Company/Manage/view/est_id/MTM3Mjk0' target='_blank'>View Details<\/a>"],['Rotana Junior Golf League',24.4433025744182,54.3767662975431,"<a href='/ems/public/Company/Manage/view/est_id/NzY5MA==' target='_blank'>View Details<\/a>"],['Rotana Mall',24.4702749578096,54.3424631693569,"<a href='/ems/public/Company/Manage/view/est_id/MzMzOQ==' target='_blank'>View Details<\/a>"],['Rotana Pharmacy',25.07323,55.13135,"<a href='/ems/public/Company/Manage/view/est_id/MTM1Mjc5' target='_blank'>View Details<\/a>"],['Saadiyat Rotana Resort & Spa',24.54516,54.43398,"<a href='/ems/public/Company/Manage/view/est_id/NjUwOTY=' target='_blank'>View Details<\/a>"],['Sharjah Rotana',25.36242,55.39146,"<a href='/ems/public/Company/Manage/view/est_id/NDY4MQ==' target='_blank'>View Details<\/a>"],['The Cove Rotana Resort',25.73667,55.88719,"<a href='/ems/public/Company/Manage/view/est_id/NjM5NDk=' target='_blank'>View Details<\/a>"],['Towers Rotana',25.21300,55.27517,"<a href='/ems/public/Company/Manage/view/est_id/NTE5OA==' target='_blank'>View Details<\/a>"],['Villa Rotana',25.1945554780764,55.2614536265911,"<a href='/ems/public/Company/Manage/view/est_id/NjA4MTQ=' target='_blank'>View Details<\/a>"],['Yas Island Rotana',24.46628,54.59933,"<a href='/ems/public/Company/Manage/view/est_id/NjU0MjM=' target='_blank'>View Details<\/a>"],['Zen The Spa At Rotana',24.4945924700841,54.3841634196444,"<a href='/ems/public/Company/Manage/view/est_id/NjMzMDY=' target='_blank'>View Details<\/a>"], ];
function initialize() {
var mapOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: Duabi
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var infowindow = new google.maps.InfoWindow();
var marker, i;
var mybounds = new google.maps.LatLngBounds();
var latlng=new Array();
var latlng = [
new google.maps.LatLng(24.2187077269409, 55.7486816157711),
new google.maps.LatLng(25.2483086562942, 55.3453971796322),
new google.maps.LatLng(25.2695672065064, 55.3171133437058),
new google.maps.LatLng(25.26751, 55.31715),
new google.maps.LatLng(25.2689526605375, 55.3169359915134),
new google.maps.LatLng(24.4921202648678, 54.3674452492895),
new google.maps.LatLng(25.2027734837341, 55.2778133352192),
new google.maps.LatLng(24.4691980739652, 54.3723774450805),
new google.maps.LatLng(25.0734792101092, 55.1308173703686),
new google.maps.LatLng(25.09568, 55.15381),
new google.maps.LatLng(24.4945924700841, 54.3841634196444),
new google.maps.LatLng(25.2518237482319, 55.3014216739748),
new google.maps.LatLng(24.41996, 54.44185),
new google.maps.LatLng(24.41982, 54.44219),
new google.maps.LatLng(25.50868, 56.36336),
new google.maps.LatLng(24.2187077269409, 55.7486816157711),
new google.maps.LatLng(24.50123, 54.37793),
new google.maps.LatLng(24.27384, 55.77954),
new google.maps.LatLng(24.27357, 55.77900),
new google.maps.LatLng(24.4974871914531, 54.373612287925),
new google.maps.LatLng(25.2373282998585, 55.274198112447),
new google.maps.LatLng(24.45975, 54.31256),
new google.maps.LatLng(24.4756970326267, 54.3218642298077),
new google.maps.LatLng(25.0998417925132, 55.179632179501),
new google.maps.LatLng(25.0998417925132, 55.179632179501),
new google.maps.LatLng(25.71865, 55.84713),
new google.maps.LatLng(25.12265, 56.32716),
new google.maps.LatLng(24.42406, 54.47348),
];
//alert(JSON.stringify(latlng));
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
title: locations[i][0]
});
alert(latlng[i]);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent('<b>'+locations[i][0]+'</b></br>'+locations[i][3]);
infowindow.open(map, marker);
mybounds.extend(latlng[i]);
}
})(marker, i));
}
map.fitBounds(mybounds);
}
</script>
但它没有缩放以适应标记。标记正从数据库加载到脚本。我需要在地图上设置其中心点,并且必须占据地图中的所有标记。请告诉我错过的地方。
提前致谢
答案 0 :(得分:0)
您正在扩展侦听器内的边界。尝试移动
mybounds.extend(latlng[i]);
在听众之外
mybounds.extend(latlng[i]);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent('<b>'+locations[i][0]+'</b></br>'+locations[i][3]);
infowindow.open(map, marker);
}
})(marker, i));
另一方面,,您的latlng数组不包含与您的位置数组一样多的位置,因此您将无法拟合地图中的所有位置,仅适用于latlng。