Automatically refresh layer ?

4,075 views
Skip to first unread message

John York

unread,
Oct 23, 2012, 12:18:27 PM10/23/12
to leafl...@googlegroups.com
Hello
I want to automatically refresh layer, but I get this error :Uncaught TypeError: Cannot read property '_leaflet_id' of undefined

The code is:

<script type='text/javascript'>
var track;
var map;
function initmap() {
map = new L.Map('map');
// create the tile layer with correct attribution
var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © OpenStreetMap contributors';
var osm = new L.TileLayer(osmUrl, {minZoom: 5, maxZoom: 12, attribution: osmAttrib});
// start the map in South-East England
map.setView(new L.LatLng(48.4, 13.0),5);
map.addLayer(osm);
loadLayer();
refreshLayer();
}
function loadLayer() {
var track = new L.KML("genkml.php", {async: true});
map.addLayer(track);
}
function refreshLayer() {
map.removeLayer(track);
loadLayer();
timeOut=setTimeout("refreshLayer()",10000);
}
</script>

where is the problem?
Please help me.... Thank You!!!



Adam Eskreis

unread,
Oct 25, 2012, 6:22:04 PM10/25/12
to leafl...@googlegroups.com
I may be wrong on this but I think this line:


var track = new L.KML("genkml.php", {async: true});

Because you are saying "var track" instead of just "track" it is using a local version of "track" instead of the global version.  As a result, the global "track" variable is null and thus fails when you try to remove it.  Try removing the "var" keyword and see if that fixes your problem.

John York

unread,
Oct 26, 2012, 12:08:15 PM10/26/12
to leafl...@googlegroups.com
Thank you very much
Аdjustment was successful!!!
working code is:

<script type='text/javascript'>
var track;
var map;
function initmap() {
map = new L.Map('map');
var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © OpenStreetMap contributors';
var osm = new L.TileLayer(osmUrl, {minZoom: 5, maxZoom: 12, attribution: osmAttrib});
map.setView(new L.LatLng(48.4, 13.0),5);
map.addLayer(osm);
refreshLayer();
}
function refreshLayer() {
map.removeLayer(track);
track = new L.KML("genkml.php", {async: true});
map.addLayer(track);
timeOut=setTimeout("refreshLayer()",10000);
}
</script>

this is a test for automatic recharging of the KML layer

Thank you very much

glangaas

unread,
Dec 25, 2013, 5:14:11 PM12/25/13
to leafl...@googlegroups.com
Hello, I have a similar problem - When adding some points to the map I would like to update them every 5 sec. When  adding the code it seems like the browser are running out of memory within a few minutes. Please advice. Or am I doing something that is possible?

 var map;

function init() {

map = L.map('map').setView([54.55004,11.7428], 13);
 
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var loadData = function() {
map.removeLayer(L.circleMarker);
map.removeLayer(L.geoJson);

var bodyContent = $.ajax({
  url: "connectdong.php",
  global: true,
  type: "POST",
  data: "name=value",
  dataType: "json",
  async:false,
  success: function(msg){
 
  }
}).responseText;
var mills_geojson = "var mills_geojson = " + bodyContent;
eval(mills_geojson);
var on_mill = {
    radius: 8,
    fillColor: "#FF0000",
    color: "#000",
    weight: 1,
    opacity: 1,
    fillOpacity: 1
};
var off_mill = {
    radius: 8,
    fillColor: "#00FF00",
    color: "#000",
    weight: 1,
    opacity: 1,
    fillOpacity: 1
};
function onEachFeature(feature, layer) {
if (feature.properties && feature.properties.radio ) {
        layer.bindPopup(feature.properties.radio);
        }
        if (feature.properties && feature.properties.nix ) {
        layer.bindPopup(feature.properties.nix);
        }
}
L.geoJson(mills_geojson, {
onEachFeature: onEachFeature,
pointToLayer: function (feature, latlng) {
if (feature.properties.radio){
        return L.circleMarker(latlng, on_mill);
        }
        else{
        return L.circleMarker(latlng, off_mill);
        }
    }
}).addTo(map);
}
loadData();
setInterval(loadData, 5000); //5 seconds
};


How to prevent memory loss. Anybody ??
Reply all
Reply to author
Forward
0 new messages