Google 地图 - 地图重新加载
Google Maps - Map reload
我有以下代码设置来为各种区域应用地图
var locations = [
['Liver Office - Liverpool Office', 53.40529, -2.988801, 1],
['Lond office - London Office', 51.515026, -0.086811, 2],
];
function plotMap(loc) {
var mapOptions = {
zoom: 17,
center: new google.maps.LatLng((locations[loc][1]), (locations[loc][2])),
stylers: [
{ saturation: -100 } // <-- THIS
]
};
var map = new google.maps.Map(document.getElementById('map'),
mapOptions);
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'tehgrayz']
},
icon: 'marketICO.png',
title: (locations[loc][0])
});
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', (function(marker) {
return function() {
infowindow.setContent(locations[loc][0]);
infowindow.open(map, marker);
}
})(marker, loc));
}
$('.livLink').click(function(){
plotMap(0);
});
$('.lonLink').click(function(){
plotMap(1);
});
plotMap(0);
关于重新加载地图 - 目前上面的脚本由 2 个选项卡按钮触发 - 如果加载地图并单击第二个按钮,脚本将重新运行并替换现有地图 - 我只是在想内存问题 - 是否应该在加载第二个之前停止初始地图实例?
您可以创建 2 个地图实例(例如 map1
和 map2
)。
在文档就绪(或其他事件)时初始化两个地图,并在更改选项卡时触发地图调整大小。
google.maps.event.trigger(map, 'resize');
将 map
替换为适当的地图对象(对应于您显示的选项卡上的地图)。
当您考虑内存问题(以及不考虑)时,最好重新使用 Map 实例(参见:Bug: Destroying Google Map Instance Never Frees Memory)
function plotMap(loc) {
var map_container = document.getElementById('map');
if (!map_container.map) {
map_container.map = new google.maps.Map(map_container,
{
stylers: [{
saturation: -100
}
]
});
map_container.marker = new google.maps.Marker();
map_container.infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(map_container.marker, 'click', function () {
map_container.infowindow.close();
map_container.infowindow.open(this.getMap(), this);
});
map_container.infowindow.bindTo('content', map_container.marker, 'content');
}
map_container.infowindow.close();
map_container.map.setOptions({
zoom: 17,
center: new google.maps.LatLng((locations[loc][1]), (locations[loc][2]))
});
//icon: 'marketICO.png',
map_container.marker.setOptions({
position: map_container.map.getCenter(),
map: map_container.map,
content: locations[loc][0],
title: locations[loc][0]
});
}
我有以下代码设置来为各种区域应用地图
var locations = [
['Liver Office - Liverpool Office', 53.40529, -2.988801, 1],
['Lond office - London Office', 51.515026, -0.086811, 2],
];
function plotMap(loc) {
var mapOptions = {
zoom: 17,
center: new google.maps.LatLng((locations[loc][1]), (locations[loc][2])),
stylers: [
{ saturation: -100 } // <-- THIS
]
};
var map = new google.maps.Map(document.getElementById('map'),
mapOptions);
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'tehgrayz']
},
icon: 'marketICO.png',
title: (locations[loc][0])
});
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', (function(marker) {
return function() {
infowindow.setContent(locations[loc][0]);
infowindow.open(map, marker);
}
})(marker, loc));
}
$('.livLink').click(function(){
plotMap(0);
});
$('.lonLink').click(function(){
plotMap(1);
});
plotMap(0);
关于重新加载地图 - 目前上面的脚本由 2 个选项卡按钮触发 - 如果加载地图并单击第二个按钮,脚本将重新运行并替换现有地图 - 我只是在想内存问题 - 是否应该在加载第二个之前停止初始地图实例?
您可以创建 2 个地图实例(例如 map1
和 map2
)。
在文档就绪(或其他事件)时初始化两个地图,并在更改选项卡时触发地图调整大小。
google.maps.event.trigger(map, 'resize');
将 map
替换为适当的地图对象(对应于您显示的选项卡上的地图)。
当您考虑内存问题(以及不考虑)时,最好重新使用 Map 实例(参见:Bug: Destroying Google Map Instance Never Frees Memory)
function plotMap(loc) {
var map_container = document.getElementById('map');
if (!map_container.map) {
map_container.map = new google.maps.Map(map_container,
{
stylers: [{
saturation: -100
}
]
});
map_container.marker = new google.maps.Marker();
map_container.infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(map_container.marker, 'click', function () {
map_container.infowindow.close();
map_container.infowindow.open(this.getMap(), this);
});
map_container.infowindow.bindTo('content', map_container.marker, 'content');
}
map_container.infowindow.close();
map_container.map.setOptions({
zoom: 17,
center: new google.maps.LatLng((locations[loc][1]), (locations[loc][2]))
});
//icon: 'marketICO.png',
map_container.marker.setOptions({
position: map_container.map.getCenter(),
map: map_container.map,
content: locations[loc][0],
title: locations[loc][0]
});
}