var map;
var myMarkers;
var dataSet;

function addMarkers(data, mapBase, markersLayer) {
    dataSet = data;
    map = mapBase;

if(myMarkers ==null){

 myMarkers = new OpenLayers.Layer.Vector("车");
   map.addLayer(markers);

}
    myMarkers = markersLayer;
    add();
}

function add() {

    var size = new OpenLayers.Size(30, 30);
    var calculateOffset = function (size) {
        return new OpenLayers.Pixel(-(size.w / 2), -(size.h / 2));
    };

    //需要修改
    for (var i = 0; i < dataSet.length; i++) {

        var d = {};
        d.id = dataSet[i].VehicleID.toString();

        var num = Math.floor(dataSet[i].Direction / 5) * 5;

       var ic = "http://www.cnblogs.com/Images/Icon/bus_blue/bus_blue_45.png";
        d.icon = new OpenLayers.Icon(
                ic,
                size, null, calculateOffset);

        var lonlat = new OpenLayers.LonLat(dataSet[i].Longitude,
                                             dataSet[i].Latitude);
        var sHtml = "<div id=\"markerNews\">"
        + "车牌号:" + dataSet[i].VehicleID + "<br />"
        + "方向:" + Math.floor(dataSet[i].Direction / 5)*5 + "<br />"
        + "速度:"+dataSet[i].Speed+"<br />"
        + "驾驶员:" + dataSet[i].DriverName + "<br />"
        + "车辆类型:" + dataSet[i].VehicleModelName
        + "</div>";
        //end

        var feature = new OpenLayers.Feature(myMarkers, lonlat, d);
        feature.closeBox = true;
        feature.data.popupContentHTML = sHtml;

        var marker = feature.createMarker();
        marker.events.register('click', feature, markerClick);
        myMarkers.addMarker(marker);
    }

    //    setTimeout(add, time);
}

//不变
function markerClick(evt) {
    var p = this.createPopup(this.closeBox);
    var ll = p.lonlat;
    var sHtml = p.contentHTML;
    var popup;
    var len = map.popups.length;
    var feature = new OpenLayers.Feature(myMarkers, ll);

    for (var j = 0; j < len; j++) {
        popup = map.popups[j];
        map.removePopup(popup);

        if (popup.feature) {
            popup.feature.popup.destroy();
            popup.feature.popup = null;
            popup.feature = null;
        }
        popup = null;
    }

    popup = new OpenLayers.Popup.FramedCloud("featurePopup",
                                             ll,
                                             new OpenLayers.Size(200, 200),
                                             sHtml,
                                             null, true);
    feature.popup = popup;
    popup.feature = feature;
    map.addPopup(popup);
    OpenLayers.Event.stop(evt);

}

Posted on 2011-08-01 14:47  萧鼎  阅读(1124)  评论(0编辑  收藏  举报