2011-03-01 54 views
0

我有一個連接到數據庫(db)。我越來越LON,緯度和名稱從DB和stroing他們:Openlayers - 彈出信息到popupmarker

while ($row_ChartRS = mysql_fetch_array($sql1)) 
{ 
$latitude=$row_ChartRS['latitude']; 
$longitude=$row_ChartRS['longitude']; 
$bus_name =$row_ChartRS['short_name']; 
//echo $latitude.'--'.$longitude.'<br>'; 
echo $bus_name; 

我然後建立一個地圖來顯示數據。標記對所有經緯度位置都能正常工作。代碼:

function init() 
    { 

    projLonLat = new OpenLayers.Projection("EPSG:4326"); // WGS 1984 
    projMercator = new OpenLayers.Projection("EPSG:900913"); // Spherical Mercator 

    overviewMap = new OpenLayers.Control.OverviewMap(); 

    //adding scale ruler 
    scale = new OpenLayers.Control.ScaleLine(); 
    scale.geodesic = true; // get the scale projection right, at least on small 

    map = new OpenLayers.Map('demoMap', 
          { controls: [ new OpenLayers.Control.Navigation(), // direct panning via mouse drag 
              new OpenLayers.Control.Attribution(), // attribution text 
              new OpenLayers.Control.MousePosition(), // where am i? 
              new OpenLayers.Control.LayerSwitcher(), // switch between layers 
              new OpenLayers.Control.PanZoomBar(), // larger navigation control 
              scale, 
              overviewMap        // overview map 
             ] 
          } 
          ); 

    map.addLayer(new OpenLayers.Layer.OSM.Mapnik("Mapnik")); 
    map.addLayer(new OpenLayers.Layer.OSM.Osmarender("Osmarender")); 


    //Create an explicit OverviewMap object and maximize its size after adding it to the map so that it shows 
    //as activated by default. 
    overviewMap.maximizeControl(); 

    //Adding a marker 
    markers = new OpenLayers.Layer.Markers("Vehicles"); 
    map.addLayer(markers); 

    vectorLayer = new OpenLayers.Layer.Vector('Routes'); 
    map.addLayer(vectorLayer); 

    for (k in Locations) 
    { 

    //adding a popup for the marker 
    var feature = new OpenLayers.Feature(markers, new OpenLayers.LonLat(Locations[k].lon, Locations[k].lat).transform(projLonLat,projMercator)); 

    //true to close the box 
    feature.closeBox = true; 

    feature.popupClass = new OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, 
    { 
     //create the size of the box 
     'autoSize': true, 
     'maxSize': new OpenLayers.Size(100,100) 
    }); 

    //add info into box 
    for (z in names) 
    { 
    feature.data.popup = new OpenLayers.Feature(new OpenLayers.LonLat(names[z]).transform(projLonLat,projMercator)); 

    } 

    //puts a scroll button on box to scroll down to txt 
    //feature.data.overflow = "auto"; 

    marker = feature.createMarker(); 
    marker.display(true); 

    markerClick = function (evt) { 
     if (this.popup == null) { 
     this.popup = this.createPopup(this.closeBox); 
     map.addPopup(this.popup); 
     this.popup.show(); 
     } else { 
     this.popup.toggle(); 
     } 
     currentPopup = this.popup; 
     OpenLayers.Event.stop(evt); 
    }; 

    marker.events.register("mousedown", feature, markerClick); 
    markers.addMarker(marker); 
    map.setCenter(new OpenLayers.LonLat(Locations[k].lon, Locations[k].lat).transform(projLonLat,projMercator), zoom); 

    var lonLat1 = new OpenLayers.LonLat(Locations[k].lon,Locations[k].lat).transform(new OpenLayers.Projection('EPSG:4326'), map.getProjectionObject()); 
    var pos2=new OpenLayers.Geometry.Point(lonLat1.lon,lonLat1.lat); 
    points1.push(pos2); 


    //Uncomment to put boxes in when map opens 
    //feature.popup = feature.createPopup(feature.closeBox); 
    //map.addPopup(feature.popup); 
    //feature.popup.show() 
     } 

    var lineString = new OpenLayers.Geometry.LineString(points1); 
    var lineFeature = new OpenLayers.Feature.Vector(lineString,'',style_green); 
    vectorLayer.addFeatures([lineFeature]); 
    map.setCenter(lonLat1,zoom); 

    } //function 

但是,彈出標記中的名稱對所有標記都是相同的。即從數據庫中取出的姓氏。任何人都可以請幫助 - 我花了整整3天的時間來修復它!

在此先感謝!

回答

0

幾點意見:

  1. 你張貼的PHP代碼是完全不相干的,因爲它沒有看到任何地方使用。
  2. 對象namesLocations未在您發佈的代碼中的任何位置聲明。它們包含什麼?
  3. 在下面引用的代碼中,您正在創建多個新的Feature對象,但是將它們全部分配給相同的屬性(從而每次都覆蓋該屬性)。那是故意的嗎?

    //add info into box 
    for (z in names) { 
        feature.data.popup = new OpenLayers.Feature(new OpenLayers.LonLat(names[z]).transform(projLonLat,projMercator)); 
    } 
    

編輯: 這確實出現了到哪裏去錯誤的。您應該刪除了... Z循環,並用下面的代碼替換:

//add info into box 
feature.data.popup = new OpenLayers.Feature(new OpenLayers.LonLat(names[k]).transform(projLonLat,projMercator)); 

因爲在PHP,您使用的是相同的指數($v)填寫兩個陣列,它是有道理的使用相同指數從除了閱讀他們的JavaScript ...


,使用JavaScript的數組for...in循環不被認爲是很好的做法,爲a number of reasons。這是更好地使用以下命令:

for (k = 0; k < Locations.length; k += 1) { 
    // your code 
} 
+0

@ Martijn謝謝你的迴應。請參閱上面的代碼,看看它們是如何使用的。 – user639439 2011-03-01 14:34:20

+0

@ user639439:感謝您的PHP代碼。我已經更新了我的答案;如果您按照建議更改代碼,它應該可以工作。 – Martijn 2011-03-01 15:07:14

0

我有同樣的問題,我解決它... 問題是覆蓋 你不必循環的功能裏面,做循環爲功能例如:

function init(z) 
{ 
    feature.data.popup = new OpenLayers.Feature(new OpenLayers.LonLat(names[z]).transform(projLonLat,projMercator)); 
} 


for (z in names) 
{ 
    init(z) 
}