how to add markers with OpenLayers 3

后端 未结 3 1682
孤独总比滥情好
孤独总比滥情好 2020-12-04 12:42

I\'m trying to add makers on a OpenLayers 3 map.

The only example I have found is this one in the OpenLayers example list.

But the

3条回答
  •  天涯浪人
    2020-12-04 13:32

    there's a good tutorial at: http://openlayersbook.github.io

    not tested but may helpful

    var features = [];
    
    //iterate through array...
    for( var i = 0 ; i < data.length ; i++){
        var item = data[i];                                     //get item
        var type = item.type;                                   //get type
        var longitude = item.longitude;                         //coordinates
        var latitude = item.latitude;
        /*....
        * now get your specific icon...('..../ic_customMarker.png')
        * by e.g. switch case...
        */
        var iconPath = getIconPath(type);
    
        //create Feature... with coordinates
        var iconFeature = new ol.Feature({
            geometry: new ol.geom.Point(ol.proj.transform([longitude, latitude], 'EPSG:4326',     
            'EPSG:3857'))
        });
    
        //create style for your feature...
        var iconStyle = new ol.style.Style({
            image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
            anchor: [0.5, 46],
            anchorXUnits: 'fraction',
            anchorYUnits: 'pixels',
            opacity: 0.75,
            src: iconPath
            }))
        });
    
        iconFeature.setStyle(iconStyle);
        features.push(iconFeature);
        //next item...
    }
    
    /*
    * create vector source
    * you could set the style for all features in your vectoreSource as well
    */
    var vectorSource = new ol.source.Vector({
        features: features      //add an array of features
        //,style: iconStyle     //to set the style for all your features...
    });
    
    var vectorLayer = new ol.layer.Vector({
        source: vectorSource
    });
    map.addLayer(vectorLayer);
    

提交回复
热议问题