这是我的代码,我想根据地图上的标记位置调整自动缩放和居中位置,以便每个标记都能在最小可能缩放时隐藏
var mapOptions = {
center: new google.maps.LatLng('<?php echo $latitude; ?>', '<?php echo $longitude; ?>'),
zoom: 12,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
答案 0 :(得分:0)
希望这有帮助。
var zoom = 12;
var lat1 = '<?php echo $first_lat; ?>';
var lat2 = '<?php echo $latitude; ?>';
var lon1 = '<?php echo $first_lng; ?>';
var lon2 = '<?php echo $longitude; ?>';
var unit = 'M';
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist
}
var dis = distance(lat1, lon1, lat2, lon2, unit);
// change value according to your needs.
if (dis > '2.0' && dis < '3.9'){
zoom = 11
} else if (dis > '4.0' && dis < '5.9'){
zoom = 10;
} else if (dis > '6.0' && dis < '9.9'){
zoom = 9;
} else if (dis > '10.0'){
zoom = 8;
}
var mapOptions = {
center: new google.maps.LatLng(lat1,lon1),
zoom: zoom,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
{
/*
FOLLOW YOUR OTHER MAP CODE <../>
*/
}
由于