Question

function init_map() {
    var map, layer;
    map = new OpenLayers.Map('map');
    layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
    map.addLayer(layer);
    map.setCenter(new OpenLayers.LonLat(43.5, 40), 10);
    var size = new OpenLayers.Size(21,25);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',size,offset);
    var markers = new OpenLayers.Layer.Markers("Objects");
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.addLayer(markers);
    $.ajax({
        type: "GET",
        url: "/rest/displaymap/getMarkers/",
        dataType: "json",
        error: function() {
            console.log('failed to retrieve data');
        },
        success: function(d) {
            console.log(d);
            var i;
            for(i=0; i<d.length; i++) {
                var mrk = new OpenLayers.Marker(new OpenLayers.LonLat(d[i]['objectLon'],d[i]['objectLat']), icon);
                markers.addMarker(mrk);
            }    
        }
    });
}

^ my Init() function. I have a simple layer with markers and tiles layer. Returned json is correct. But only one marker is displayed instead of all. Dunno where is the problem. Help please!

Was it helpful?

Solution

You should use icon.clone() in this case. In Marker documentation it is written:

Note that if you pass an icon into the Marker constructor, it will take that icon and use it. This means that you should not share icons between markers -- you use them once, but you should clone() for any additional markers using that same icon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top