2011-07-22 68 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()"> 

如何在更新新標記2後刪除以前的標記2?如何刪除以前的標記?

回答

0

在使用新標記添加新圖層之前,應該使用舊標記去除圖層。所以,

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

之前,你應該叫

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

這是相當矯枉過正,但。您應該改爲只使用一個標記圖層,並使用方法addMarkerremoveMarker

從中添加/刪除標記