1

I just started worrking with OpenLayers and hava a problem with markers that don´t appear. I have no idea where is the problem.

<script type="text/javascript">

function init() {
    var center = new OpenLayers.LonLat(17.55, 53.7).transform('EPSG:4326', 'EPSG:900913');
    var map = new OpenLayers.Map("map");
    var osm = new OpenLayers.Layer.OSM();
    map.addLayer(osm);
    map.setCenter(center, 12);


    var markers = new OpenLayers.Layer.Markers("Markers");
    map.addLayer(markers);
    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 table;

    console.log(map);


$.getJSON('url',

function (data) {
    console.log(data);
    $.each(data.response.venues, function (i, venues) {

        markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(venues.location.lng, venues.location.lat).transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913")), icon));
        markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(venues.location.lng, venues.location.lat).transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913")), icon.clone()));
    });
});
}    

1 UPDATE: map.addLayers(markers) changed to map.addLayer(markers) 2 UPDATE: put the getJSON call in the init() function

1
  • Should you have left your client_id and client_secret in the URL like that?
    – Spacedman
    Dec 28, 2013 at 12:22

1 Answer 1

3

Two things. First, all your variables defined in init() are out of scope when the JSON function runs. Fix that by putting the getJSON call in the init() function too, or defining the map and other variables in the global scope (especially if you want to do anything else with them).

Secondly, map.addLayer(markers) instead of map.addLayers(markers). There's only one marker layer, so use the singular.

3
  • i corrected the second suggestion but as i´m new to web programming i don´t really know how to put the getJSON call in the init() function. Any suggestion where i can find some information about it, as so far i haven't found anything.
    – bucek
    Dec 30, 2013 at 23:44
  • Just put the curly bracket that closes the init function just before the closing </script> tag. Then all the JS in the script tags is one big init file which gets run "onLoad" as specified in your HTML.
    – Spacedman
    Dec 31, 2013 at 8:04
  • That works!!! thanx
    – bucek
    Jan 1, 2014 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.