Javascript 谷歌地图API v3-显示所有可见标记,最大缩放
我有一个页面,可以在其中打开/关闭标记类别。一个标记(“属性”)将始终可见。我希望尽可能放大显示所有可见标记 因此,如果我有3个标记在一起,我希望在尽可能地放大所有标记的同时仍然适合3个标记Javascript 谷歌地图API v3-显示所有可见标记,最大缩放,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一个页面,可以在其中打开/关闭标记类别。一个标记(“属性”)将始终可见。我希望尽可能放大显示所有可见标记 因此,如果我有3个标记在一起,我希望在尽可能地放大所有标记的同时仍然适合3个标记 var fullBounds = new google.maps.LatLngBounds(); var map; var infowindow; var markers = {}; var nearbyPlaces = {{#property}}{{{stringify nearbyPlaces}}}{{
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);
var fullBounds=new google.maps.LatLngBounds();
var映射;
var信息窗口;
var标记={};
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}};
函数初始化(){
map=new google.maps.map(document.getElementById('map-canvas'){
中心:物业,,
缩放:15
});
var marker=new google.maps.marker({
地图:地图,
图标:'../../../../img/map/property.png',//将属性设置为绿色标记
职位:物业
});
infowindow=new google.maps.infowindow();
//为属性设置infowindow
google.maps.event.addListener(标记,'click',函数(){
infowindow.setContent(name+'
'+prop.street+'
'+prop.city+'、'+prop.state.name);
打开(地图,这个);
});
var l=nearbyPlaces.length;
而(l--){
标记[nearbyPlaces[l].类别]=[];
createCategory(nearbyPlaces[l]);
}
log(map.getZoom());
}
函数createCategory(项){
var l=item.places.length;
而(l--){
var marker=createMarker(item.places[l]);
标记[项目.类别].推送(标记);
console.log(item.category);
开关(项目类别){
“公立学校”案例:
marker.icon='../../../../img/map/public_school.png';
打破
“学院”案例:
marker.icon='../../../../img/map/college.png';
打破
“首选雇主”案例:
marker.icon='../../../../img/map/work.png';
打破
违约:
marker.icon='../../../../img/map/star.png'
}
}
}
功能切换类别(名称,el){
//map.fitBounds(fullBounds);
变量按钮=$(el);
可见值=真值;
if(button.hasClass('active')){
可见=假;
}
toggleClass('active');
变量类别=标记[名称];
对于(变量x=0;x '+place.address.street+'
'+place.address.city+','+place.address.state.name);
打开(地图,这个);
});
返回标记;
}
函数showVisible(){
//只适合可见的标记
}
google.maps.event.addDomListener(窗口“加载”,初始化);
这两行对我来说很有用:
bounds.extend(marker.position);
map.fitBounds(bounds);
你必须把它改编成你的剧本,因为这是直接从我的剧本中选出来的
希望这有帮助 这两行对我来说很有意思:
bounds.extend(marker.position);
map.fitBounds(bounds);
你必须把它改编成你的剧本,因为这是直接从我的剧本中选出来的
希望这有帮助 我所做的是在创建映射实例之后,在创建任何标记实例之前创建一个实例。然后,在创建标记时,为创建的每个标记调用LatLngBounds实例的extend方法:
myLatLngBounds.extend( myMarker.getPosition() );
完成所有标记后,我在地图实例上调用fitBounds,并将LatLngBounds传递给它:
myMap.fitBounds( myLatLngBounds );
我所做的是在创建地图实例之后,在创建任何标记实例之前创建一个实例。然后,在创建标记时,为创建的每个标记调用LatLngBounds实例的extend方法:
myLatLngBounds.extend( myMarker.getPosition() );
完成所有标记后,我在地图实例上调用fitBounds,并将LatLngBounds传递给它:
myMap.fitBounds( myLatLngBounds );
声明一个新的Google maps LatLngBounds对象
var mapLatLngBounds = new google.maps.LatLngBounds();
。。。然后,为每个标记扩展贴图边界
mapLatLngBounds.extend(marker.position);
对于地图,根据我们现在定义的边界设置中心和边界
myMap.setCenter(mapLatLngBounds.getCenter());
myMap.fitBounds(mapLatLngBounds);
标记位置是板条对象,这就是为什么可以将它们直接传递给extend函数的原因。如果您正在处理以某种方式指定lat和lng的自定义位置对象,那么在循环/创建位置时,您需要创建可以传递到extend函数的LatLng对象:
var latlng = new google.maps.LatLng(myLocations[i].Latitude, myLocations[i].Longitude);
mapLatLngBounds.extend(latlng);
声明一个新的Google maps LatLngBounds对象
var mapLatLngBounds = new google.maps.LatLngBounds();
。。。然后,为每个标记扩展贴图边界
mapLatLngBounds.extend(marker.position);
对于地图,根据我们现在定义的边界设置中心和边界
myMap.setCenter(mapLatLngBounds.getCenter());
myMap.fitBounds(mapLatLngBounds);
标记位置是板条对象,这就是为什么可以将它们直接传递给extend函数的原因。如果您正在处理以某种方式指定lat和lng的自定义位置对象,那么在循环/创建位置时,您需要创建可以传递到extend函数的LatLng对象:
var latlng = new google.maps.LatLng(myLocations[i].Latitude, myLocations[i].Longitude);
mapLatLngBounds.extend(latlng);
这不适用于所有创建的标记吗?我如何才能将此应用于visi的fitBounds