我有一个页面可以打开/关闭标记类别。一个标记('属性')将始终可见。我想放大尽可能接近显示所有可见标记。
所以,如果我有3个标记靠近在一起,我想尽可能放大所有近距离,同时仍然适合3个标记。
var fullBounds = new google.maps.LatLngBounds();
var map;
var infowindow;
var markers = {};
var nearbyPlaces = {{#property}}{{{stringify nearbyPlaces}}}{{/property}};
var property = new google.maps.LatLng({{#property}}{{address.geo.lat}},{{address.geo.lng}}{{/property}});
var name = {{#property}}{{{stringify name}}}{{/property}}
var prop = {{#property}}{{{stringify address}}}{{/property}};
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: property,
zoom: 15
});
var marker = new google.maps.Marker({
map: map,
icon: '../../../../img/map/property.png', // Set Property to a green marker
position: property
});
infowindow = new google.maps.InfoWindow();
// Set infowindow for the property
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(name + '<br/>' + prop.street + '<br/>' + prop.city + ', ' + prop.state.name);
infowindow.open(map, this);
});
var l = nearbyPlaces.length;
while (l--) {
markers[nearbyPlaces[l].category] = [];
createCategory(nearbyPlaces[l]);
}
console.log(map.getZoom());
}
function createCategory(item) {
var l = item.places.length;
while (l--) {
var marker = createMarker(item.places[l]);
markers[item.category].push(marker);
console.log(item.category);
switch(item.category){
case 'Public Schools':
marker.icon = '../../../../img/map/public_school.png';
break;
case 'Colleges':
marker.icon = '../../../../img/map/college.png';
break;
case 'Preferred Employers':
marker.icon = '../../../../img/map/work.png';
break;
default:
marker.icon = '../../../../img/map/star.png'
}
}
}
function toggleCategory(name, el) {
//map.fitBounds(fullBounds);
var button = $(el);
var visible = true;
if (button.hasClass('active')) {
visible = false;
}
button.toggleClass('active');
var category = markers[name];
for (var x = 0; x < category.length; x++){
var lat = category[x].position.k;
var lng = category[x].position.B;
var point = new google.maps.LatLng(lat,lng);
fullBounds.extend(point);
}
var l = category.length;
console.log('cagegory length: ' + category.length);
while (l--) {
category[l].setVisible(visible);
}
showVisible();
}
function createMarker(place) {
//var lat = parseFloat(place.address.geo.lat);
//var lng = parseFloat(place.address.geo.lng);
//var point = new google.maps.LatLng(lat,lng);
//fullBounds.extend(point);
//console.log(place);
var marker = new google.maps.Marker({
map: map,
title: place.title,
icon: 'http://maps.google.com/mapfiles/ms/micons/red-dot.png', // Set all other markers to red...
position: new google.maps.LatLng(place.address.geo.lat, place.address.geo.lng)
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(place.title + '<br/>' + place.address.street + '<br/>' + place.address.city + ', ' + place.address.state.name);
infowindow.open(map, this);
});
return marker;
}
function showVisible() {
// FIT ONLY VISIBLE MARKERS
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:6)
我所做的是在创建Map实例之后,在创建任何Marker实例之前创建一个LatLngBounds实例。然后在创建标记时,我为每个创建的Marker调用LatLngBounds实例的extend方法:
myLatLngBounds.extend( myMarker.getPosition() );
在制作完所有标记之后,我在我的地图实例上调用fitBounds并将其传递给LatLngBounds:
myMap.fitBounds( myLatLngBounds );
答案 1 :(得分:2)
这两行为我做了诀窍:
bounds.extend(marker.position);
map.fitBounds(bounds);
你将不得不将其改编为你的剧本,因为这是直接来自我的一个。
希望这有帮助!
答案 2 :(得分:0)
声明新的Google地图LatLngBounds对象
var mapLatLngBounds = new google.maps.LatLngBounds();
...然后为每个标记扩展地图边界
mapLatLngBounds.extend(marker.position);
对于地图,根据我们现在定义的范围设置中心和边界
myMap.setCenter(mapLatLngBounds.getCenter());
myMap.fitBounds(mapLatLngBounds);
标记位置是LatLng对象,这就是为什么你可以直接将它们传递给 extend 函数。如果您正在以某种方式处理指定 lat 和 lng 的自定义位置对象,那么当您循环/创建位置时,您需要创建可以传递给 extend 函数的LatLng对象:
var latlng = new google.maps.LatLng(myLocations[i].Latitude, myLocations[i].Longitude); mapLatLngBounds.extend(latlng);