English 中文(简体)
how to delay marker loading openlayers map with mapstraction
原标题:

I have a page with displays 10 markers in an openlayers map used via mapstraction.

I want to load map first then marker one by one say after every second, I tried using window.setTimeout() it loads the map and loads only first marker after that it stops.

  var map;
  var lat = new Array() ;
  lat=${latitude};
  var lon= new Array();
  lon=${longitude};
  var oneByOneCounter=0;
  var count=10;
  function initMap(){  
       map = new Mapstraction( mymap , openlayers );
       map.setCenter(new LatLonPoint(0.0,0.0));
       map.addControls({pan: true, zoom: small , map_type:true});
       renderMarkerOneByOne();
       map.autoCenterAndZoom();
     };


   function renderMarkerOneByOne() {
   if (oneByOneCounter < count) {
       latitude= lat[oneByOneCounter];
       longitude= lon[oneByOneCounter];
       var point = new LatLonPoint(latitude,longitude);
       var marker = new Marker(point);
       var info = "("+(oneByOneCounter+1)+")";
       marker.setInfoBubble(info);
       marker.setHover(true);
       marker.setIcon( icon_green.png , [27,31]);
       map.addMarker(marker);    
       oneByOneCounter++;
       window.setTimeout("renderMarkerOneByOne()", 1000);
     } else {
       oneByOneCounter = 0;
    }
   }

I am not able to figure out where I am doing wrong renderMarkerOneByOne() function executes properly and on putting alert() I can see the marker object is getting created all the time but, for some reason after the first marker gets plotted the other markers are not getting plotted on the map.

Any help or suggestion is welcomed

Thanks

问题回答

Instead of setTimeout(), try setInterval().

It has to be something like this

var map;
var lat = new Array() ;
lat=${latitude};
var lon= new Array();
lon=${longitude};
var oneByOneCounter=0;
var count=10;
var centerPoint;
function initMap(){  
   map = new Mapstraction( mymap , openlayers );    
   map.addControls({pan: true, zoom: small , map_type:true});
  centerPoint= new LatLonPoint(lat[0],lon[0]);// or any point anyone wants to put center
   renderMarkerOneByOne();
   map.setCenterAndZoom(centerPoint,"desired zoom level");
 };

I found the solution after wasting too much time on debugging the code. Lots of mapstraction functions doesn t work properly with openlayers api





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签