I use AngularJS in a webapp, in this webapp I have implanted a map where it shows my clients from a marker. The map I'm using is Leaflet ( link ) with a directive for AngularJS ( link ).
My problem is this, when I first access the map, it works normally, when I change the route, it erases all the markers, returning the map screen, it loads without the markers and they are shown only when I reload the page.
Function $ window.location.reload ().
So, I had the idea of using $ window.location.reload () when accessing the page where the map is located. I call the function when I click the menu icon (shortcut) of the page referring to the map and the page is reloaded showing the markers again.
vm.recarregarRota = function (){
$window.location.reload();
}
View: This is the code that loads the map.
<div class="col-md-12 box_map" style="padding: 20px 30px 20px 30px;">
<div id="recent_activity" class="box_whiteframe_map">
<leaflet defaults="vm.defaults" lf-center="vm.center" ng-init="vm.buscaEnderecoClientesEmpresas()" markers="vm.markers" width="100%" height="480px"></leaflet>
</div>
</div>
Controller: In the controller is the function used to load the DB data and assign it to the markers. It is also in this function that I create the markers, extend the properties of the map, how to tell it that its starting position is in such a coordinate ..
Controller Consideration.
variable vm, gets $ scope.
var vm = $ scope.
vm.markers = new Array(); //CRIA MARKERS A SEREM UTILIZADOS NO MAP
vm.buscaEnderecoClientesEmpresas = function() { //Função utilizada para carregar os dados do BD e atribuir aos marcadores. Também é nesta função que crio os marcadores....
vm.items = loadSaas(Cookies.get('crm_o2_hash')); // carregar saas id
vm.items.then(function(items) { // ler array de retorno
vm.saasid = items; //Armazena ID (saasid).
var dados = {
'saasid': vm.saasid
}
relatoriosService.carregarEnderecoClientesEmpresas(dados).then(function(response) {
if (response.data != 'null') {
vm.enderecoClientesEmpresas = response.data; //ARRAY QUE VEM DO BD
angular.forEach(vm.enderecoClientesEmpresas, function(value, key) { //FOREACH UTILIZADO PARA PERCORRER ARRAY
vm.markers.push({
group: value.estado, //DADOS PROVENIENTES DO BD.
lat: value.latitude, //DADOS PROVENIENTES DO BD.
lng: value.longitude, //DADOS PROVENIENTES DO BD.
message: "teste",
icon: {
type: 'awesomeMarker',
prefix: 'fa',
icon: icon,
markerColor: color
},
label: {
options: {
noHide: true
}
}
});
});
} else {
vm.enderecoClientesEmpresas = '';
}
}, function(error) {
console.log('Erro findSemEmail: ', error);
});
});
}
angular.extend(vm, { // ESTENDE AS PROPRIEDADES DO MAP (MARCADORES, LOCALIZAÇÃO INCIAL..)
center: { // LOCALIZAÇÃO INICIAL .
lat: -27.952419,
lng: -52.211667,
zoom: 6
},
defaults: { //LAYER É O TIPO DE MAPA A SER UTILIZADO
tileLayer: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
zoomControlPosition: 'topright',
tileLayerOptions: {
opacity: 0.9,
detectRetina: true,
reuseTiles: true,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> | © <a href="http://www.openstreetmap.org/copyright">Funil PRÓ</a>',
},
}
});
My problem is as follows, when the user, instead of using the system shortcuts to navigate, use the browser back arrow, the page will not reload, there is a way to make the page reload if does it use this way?
Or maybe when you access a specific route reload the page.
PRINT'S CONSOLE:
Initial map status. With all markers loaded it is noticed that the array has been traversed 27 times (leafletDirective.layeradd).
Intheimagebelowyouwillfindthereturnoftheconsolefromwhentheroutechanges.Noticethatthelayers(markers,tiles...)areremoved.
Andlastly,inthisimagealayer(Ibelieveitisthetile,theimageofamap,inthiscaseOpenStreetMap)isloaded,butnottherestofthelayersthatwouldbethemarkers(27).