简体   繁体   中英

Can't change OpenLayers marker icon

I'm tryin to set two different kinds of images , but I always get the default one. I would like to work without a extra txt file.

var iconOh and var iconParking are the images i'm trying to set.

The answers on the site did not helped me for now.

  map = new OpenLayers.Map("mapdiv");
    map.addLayer(new OpenLayers.Layer.OSM());

 // create layer switcher widget in top right corner of map.
    var layer_switcher= new OpenLayers.Control.LayerSwitcher({});
    map.addControl(layer_switcher);

    //Set markers on page  
    var lonLatOh = new OpenLayers.LonLat( 4.479121, 51.031809 )
          .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            map.getProjectionObject() // to Spherical Mercator Projection
          );
    var lonLatParking1 = new OpenLayers.LonLat( 4.476497, 51.031157 )
          .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            map.getProjectionObject() // to Spherical Mercator Projection
          );      
    var lonLatParking2 = new OpenLayers.LonLat( 4.477716, 51.029520 )
          .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            map.getProjectionObject() // to Spherical Mercator Projection
          );    

    // zoom level     
    var zoom=17;

    // Marker images
    var iconOh = new OpenLayers.Icon('oh.png');
    var iconParking = new OpenLayers.Icon('parking.png');

    // Make layer with markers
    var markers = new OpenLayers.Layer.Markers( "Oh!" );
    map.addLayer(markers);

    // Show markers
    markers.addMarker(new OpenLayers.Marker(lonLatOh),iconOh);
    markers.addMarker(new OpenLayers.Marker(lonLatParking1),iconParking);
    markers.addMarker(new OpenLayers.Marker(lonLatParking2),iconParking);

    // Center according to specific marker
    map.setCenter (lonLatOh, zoom);  

You are passing the icon parameter at the wrong position. Instead of

markers.addMarker(new OpenLayers.Marker(lonLatOh),iconOh);

try

markers.addMarker(new OpenLayers.Marker(lonLatOh, iconOh));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM