2011-07-22 17 views
0
<script> 
// holds an instance of XMLHttpRequest 
var xmlHttp = createXmlHttpRequestObject(); 

// creates an XMLHttpRequest instance 
function createXmlHttpRequestObject() 
{ 
    // will store the reference to the XMLHttpRequest object 
    var xmlHttp; 
    // this should work for all browsers except IE6 and older 
    try 
    { 
    // try to create XMLHttpRequest object 
    xmlHttp = new XMLHttpRequest(); 
    } 
    catch(e) 
    { 
    // assume IE6 or older 
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", 
            "MSXML2.XMLHTTP.5.0", 
            "MSXML2.XMLHTTP.4.0", 
            "MSXML2.XMLHTTP.3.0", 
            "MSXML2.XMLHTTP", 
            "Microsoft.XMLHTTP"); 
    // try every prog id until one works 

    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    { 
     try 
     { 
     // try to create XMLHttpRequest object 
     xmlHttp = new ActiveXObject(XmlHttpVersions[i]); 
     } 
     catch (e) {} 
    } 
    } 
    // return the created object or display an error message 
    if (!xmlHttp) 
    <?fwrite($log,"Error creating the XMLHttpRequest object.")?>; 
    else 
    return xmlHttp; 
} 

// read a file from the server 
function process() 
{ 
    // only continue if xmlHttp isn't void 
    if (xmlHttp) 
    { 
    // try to connect to the server 
    try 
    { 
     // initiate reading a file from the server 
     xmlHttp.open("GET", "update.php?id=".$_GET['id']."", true); 
     xmlHttp.onreadystatechange = handleRequestStateChange; 
     xmlHttp.send(null); 
    } 
    // display the error in case of failure 
    catch (e) 
    { 
    <? fwrite($log,"Can't connect to server:\n")?>; 
    } 
    } 
} 


// function called when the state of the HTTP request changes 
function handleRequestStateChange() 
{ 
    // when readyState is 4, we are ready to read the server response 
    if (xmlHttp.readyState == 4) 
    { 
    // continue only if HTTP status is "OK" 
    if (xmlHttp.status == 200) 
    { 
     try 
     { 
     // do something with the response from the server 
     handleServerResponses(); 
     } 
     catch(e) 
     { 
     // display error message 
     <? fwrite($log,"Error reading the response: ");?> 
     } 
    } 
    else 
    { 
     // display status message 
    <? fwrite($log,"There was a problem retrieving the data:\n");?> 

    } 
    } 
} 

// handles the response received from the server 
function handleServerResponses() 
{ 
    // read the message from the server 
    var xmlResponse = xmlHttp.responseXML; 
    // catching potential errors with IE and Opera 
    if (!xmlResponse || !xmlResponse.documentElement) 
    throw("Invalid XML structure:\n" + xmlHttp.responseText); 
    // catching potential errors with Firefox 
var rootNodeName = xmlResponse.documentElement.nodeName; 
if (rootNodeName == "parsererror") throw("Invalid XML structure"); 
    // obtain the XML's document element 
    xmlRoot = xmlResponse.documentElement; 
    // obtain arrays with book titles and ISBNs 
    titleArray = xmlRoot.getElementsByTagName("title"); 
    isbnArray = xmlRoot.getElementsByTagName("isbn"); 
    // generate HTML output 
    // iterate through the arrays and create an HTML structure 
    latc = titleArray.item(0).firstChild.data; 
    lonc = isbnArray.item(0).firstChild.data; 
    lonc = parseFloat(lonc); 
latc = parseFloat(latc); 
    setInterval('process()',17000); 
     var lonLat1 = new OpenLayers.LonLat(lonc,latc) 
      .transform(new OpenLayers.Projection("EPSG:4326"), // преобразование из WGS 1984 
            new OpenLayers.Projection("EPSG:900913") 
              ); 
       map.setCenter(lonLat1, 16); 
       var icon = new OpenLayers.Icon('images/car.png', size, offset); 
       var size = new OpenLayers.Size(21,25); 
       var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); 
       marker2 = new OpenLayers.Layer.Markers("Car"); 
       map.addLayer(marker2); 
       marker2.addMarker(new OpenLayers.Marker(lonLat1,icon)); 
} 
function init() 
      { 

     map = new OpenLayers.Map("basicMap"); 
     var mapnik = new OpenLayers.Layer.OSM(); 
     map.addLayer(mapnik); 
     lat = <? //echo $lat ?>; 
     lon = <? //echo $lon ?>; 

      var lonLat = new OpenLayers.LonLat(lon,lat) 
         .transform(new OpenLayers.Projection("EPSG:4326"), // преобразование из WGS 1984 
            new OpenLayers.Projection("EPSG:900913") 
              ); 

      marker = new OpenLayers.Layer.Markers("Marker"); 
      map.addLayer(marker); 
      var size = new OpenLayers.Size(21,25); 
      var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); 
      var icon = new OpenLayers.Icon('images/marker-def.png', size, offset); 
      marker.addMarker(new OpenLayers.Marker(lonLat,icon.clone())); 
    } 
     </script> 
    </head> 
    <body onload="process(); init()"> 

以前のmarker2を削除するには、新しいmarker2を更新した後に?前のマーカーを削除するには?

答えて

0

新しいマーカーで新しいレイヤーを追加する前に、古いマーカーでレイヤーを削除する必要があります。だから、

marker2 = new OpenLayers.Layer.Markers("Car");  
map.addLayer(marker2); 

前には、しかし、非常にやり過ぎだ

map.removeLayer(map.getLayersByName('Marker')[0]); 

を呼び出す必要があります。代わりに、1つのマーカーレイヤーのみを使用して、マーカーを追加/削除する必要があります。addMarkerおよびremoveMarker

関連する問題