无法更改OpenLayers标记图标

时间:2014-06-18 12:39:29

标签: javascript openlayers openstreetmap

我试着设置两种不同的图像,但我总是得到默认图像。 我想在没有额外的txt文件的情况下工作。

var iconOhvar iconParking是我试图设置的图片。

网站上的答案暂时没有帮助我。

  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);  

1 个答案:

答案 0 :(得分:2)

您正在将图标参数传递到错误的位置。而不是

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

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