javascript - How to get my markers to update on a Leaflet map? -
i've created map grabs data json-source , puts marker drivers inside json-data. problem is, want these drivers position update time. so, i've created function repeats every 3 seconds. cause duplicated drivers, how markers update position, not creating one? code below, json-data doesn't work due cors.
code:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="generator" content= "html tidy linux/x86 (vers 25 march 2009), see www.w3.org" /> <title></title> <meta charset="utf-8" /> <meta name="viewport" content= "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" type= "text/css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"> </script><!--[if lte ie 8]> <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.ie.css" /> <![endif]--> <style type="text/css"> /*<![cdata[*/ body { padding: 0; margin: 0; } html, body, #map { height: 100%; } /*]]>*/ </style> </head> <body> <div id="map"></div><script src="https://www.google.com/jsapi?.js" type= "text/javascript"> </script><script src="http://cdn.leafletjs.com/leaflet-0.5/leaflet.js" type= "text/javascript"> </script><script src="leaflet-ajax.js" type="text/javascript"> </script><script type="text/javascript"> //<![cdata[ init(); //calls "grab location" function function init() { var map = l.map('map', { center: [51.505, -0.09], zoom: 13 }), marker = l.marker(map.getcenter()).addto(map) .bindpopup("<center><b>jag<\/b><\/center>").openpopup(); glcl = google.loader.clientlocation, onlocationfound = function (e) { marker.setlatlng(e.latlng); map.setview(marker.getlatlng(), map.getzoom()); }; l.tilelayer('http://{s}.tile.cloudmade.com/1fa9625d371549a298938509a2eac256/997/256/{z}/{x}/{y}.png').addto(map); map.on('locationfound', onlocationfound); if (glcl) { //when google.loader.clientlocation contains result onlocationfound({ latlng: [glcl.latitude, glcl.longitude] }); } else {} map.locate(); driversrealtime(); function driversrealtime() { setinterval(function () { var url = "data.json"; var name; var img; $.getjson(url, function (response) { name = response.drivers[0].name; img = response.drivers[0].img; (var = 0; < response.drivers.length; i++) { var driver = response.drivers[i]; var m = l.marker(new l.latlng(driver.currentlat, driver.currentlon)).addto(map) .bindpopup("<center><b>" + driver.name + "<\/b><\/center>"); } }); }, 3000); //delay millisekunder } } // api-url: http://blackcab.didair.se/api/drivers //]]> </script> </body> </html>
Comments
Post a Comment