admin管理员组文章数量:1327849
I have the following code setup to apply a map for a variety of areas
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);
Regarding reloading the map - at the moment the above script is triggered by 2 tab buttons - if a map is loaded and the second button is clicked the script re-runs and replaces the existing map - I'm just thinking of memory issues - should the initial map instance be stopped before loading a second?
I have the following code setup to apply a map for a variety of areas
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);
Regarding reloading the map - at the moment the above script is triggered by 2 tab buttons - if a map is loaded and the second button is clicked the script re-runs and replaces the existing map - I'm just thinking of memory issues - should the initial map instance be stopped before loading a second?
Share asked Feb 5, 2015 at 13:54 DancerDancer 17.7k40 gold badges131 silver badges213 bronze badges 1- Possible duplicate of How to manually reload Google Map with JavaScript – Zakaria Acharki Commented Oct 22, 2015 at 10:54
2 Answers
Reset to default 2You can create 2 map instances (map1
and map2
for example).
Initialize both maps on document ready (or another event) and trigger a map resize when changing tabs.
google.maps.event.trigger(map, 'resize');
Replace map
by the appropriate map object (corresponding to the map on the tab you show).
When you think about memory issues(and also when not) it's better to re-use the Map-instance(see: 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]
});
}
本文标签: javascriptGoogle MapsMap reloadStack Overflow
版权声明:本文标题:javascript - Google Maps - Map reload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742219893a2435256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论