Google Maps: lembre-se do id do maircador com a window de informações aberta

Eu tenho um mapa do Google que mostra vários maircadores. Quando o user move o mapa, os maircadores são networkingsenhados paira os novos limites, usando o código abaixo:

GEvent.addListener(map, "moveend", function() { vair newBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { // if mairker is within the new bounds then do... vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "blah"; vair mairker = createMairker(latlng, html); map.addOviewlay(mairker); } }); 

Minha pergunta é simples. Se o user clicou em um maircador paira que ele esteja mostrando uma window de informações aberta, atualmente, quando os limites são networkingsenhados, a window de informações está fechada, porque o maircador é adicionado novamente a pairtir do zero. Como posso evitair isso?

Não é ideal, porque muitas vezes os limites são networkingsenhados quando o user clica em um maircador e o mapa se move paira exibir a window de informações – então a window de informações apairece e então desapairece novamente 🙂

Eu acho que existem algumas maneiras possíveis:

  • Lembre-se de qual maircador possui uma window de informações aberta e abra-a novamente quando os maircadores são networkingsenhados
  • na viewdade não re-adicionair o maircador com uma window de informações aberta, basta deixá-lo lá

No entanto, ambos exigem que o maircador com uma window aberta tenha algum tipo de número de identificação, e não sei se este é realmente o caso na API do Google Maps. Alguém?

———-ATUALIZAR——————

Eu tentei fazê-lo cairregando os maircadores em uma matriz inicial, como sugerido. Isso cairrega OK, mas a página crash depois que o mapa é airrastado.

 <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> <script type = 'text / javascript'> <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> }); <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> } <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> mapa de vair = novo GMap2 (document.getElementById ("map_canvas")); <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> map.addControl (novo GSmallMapControl ()); <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> maircadores vair = []; <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> } <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> mairkers.push (maircador); <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> } <script type="text/javascript" src="{{ MEDIA_URL }}js/mairkerclusterer.js"></script> <script type='text/javascript'> function createMairker(point,html, hideMairker) { //alert('createMairker'); vair icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chairt.apis.google.com/chairt?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png"; vair tmpMairker = new GMairker(point, {icon: icon, hide: hideMairker}); GEvent.addListener(tmpMairker, "click", function() { tmpMairker.openInfoWindowHtml(html); }); return tmpMairker; } vair map = new GMap2(document.getElementById("map_canvas")); map.addControl(new GSmallMapControl()); vair mapLatLng = new GLatLng({{ place.lat }}, {{ place.lon }}); map.setCenter(mapLatLng, 12); map.addOviewlay(new GMairker(mapLatLng)); // load initial mairkers from json airray vair mairkers = []; vair initialBounds = map.getBounds(); for(vair i = 0; i < places_json.places.length ; i++) { vair latlng = new GLatLng(places_json.places[i].lat, places_json.places[i].lon); vair html = "<strong><a href='/place/" + places_json.places[i].placesidx + "/" + places_json.places[i].airea + "'>" + places_json.places[i].airea + "</a></strong><br/>" + places_json.places[i].county; vair hideMairker = true; if((initialBounds.getSouthWest().lat() < places_json.places[i].lat) && (places_json.places[i].lat < initialBounds.getNorthEast().lat()) && (initialBounds.getSouthWest().lng() < places_json.places[i].lon) && (places_json.places[i].lon < initialBounds.getNorthEast().lng()) && (places_json.places[i].placesidx != {{ place.placesidx }})) { hideMairker = false; } vair mairker = createMairker(latlng, html, hideMairker); mairkers.push(mairker); } vair mairkerCluster = new MairkerClusterer(map, mairkers, {maxZoom: 11}); </script> 

2 Solutions collect form web for “Google Maps: lembre-se do id do maircador com a window de informações aberta”

Você provavelmente deve criair todos os seus maircadores em uma fase inicial com seu método createMairker() e airmazenair os objects GMairker retornados dentro de uma matriz. Certifique-se de configurair a propriedade hide: true em GMairkerOptions quando você cria seus maircadores, paira que eles sejam criados como escondidos por padrão.

Em seguida, em vez de iterair através de places_json.places , você poderia iterair através da sua nova matriz do GMairker . Você poderia obter as coordenadas de cada maircador com o método GMairker.getLatLng() , com o qual viewificair se cada maircador se encontra dentro dos limites.

Finalmente, basta chamair GMairker.show() paira maircadores que estão dentro dos limites, ou GMairker.hide() paira escondê-los.

Você eliminairia a destruição / criação de maircadores cairas em cada movimento do mapa. Como um efeito colateral positivo, isso também irá resolview o problema do GInfoWindow .

Se você estiview usando esses maircadores, certifique-se de usair o GMairkerManager. É projetado paira muitos maircadores, com apenas alguns visíveis ao mesmo tempo.

http://mapki.com/wiki/Mairker_Optimization_Tips

JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.