how to add markers with OpenLayers 3

后端 未结 3 1667
孤独总比滥情好
孤独总比滥情好 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:26
    var exampleLoc = ol.proj.transform(
        [131.044922, -25.363882], 'EPSG:4326', 'EPSG:3857');
    
    var map = new ol.Map({
      target: 'map',
      renderer: 'canvas',
      view: new ol.View2D({
        projection: 'EPSG:3857',
        zoom: 3,
        center: exampleLoc
      }),
      layers: [
        new ol.layer.Tile({source: new ol.source.MapQuest({layer: 'osm'})})
      ]
    });
    
    map.addOverlay(new ol.Overlay({
      position: exampleLoc,
      element: $('<img src="resources/img/marker-blue.png">')
          .css({marginTop: '-200%', marginLeft: '-50%', cursor: 'pointer'})
          .tooltip({title: 'Hello, world!', trigger: 'click'})
    }));
    
    map.on('postcompose', function(evt) {
      evt.vectorContext.setFillStrokeStyle(
          new ol.style.Fill({color: 'rgba(255, 0, 0, .1)'}),
          new ol.style.Stroke({color: 'rgba(255, 0, 0, .8)', width: 3}));
      evt.vectorContext.drawCircleGeometry(
          new ol.geom.Circle(exampleLoc, 400000));
    });
    
    var exampleKml = new ol.layer.Vector({
      source: new ol.source.KML({
        projection: 'EPSG:3857',
        url: 'data/example.kml'
      })
    });
    map.addLayer(exampleKml);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-04 13:43

    Q1. Markers are considered deprecated in OpenLayers 2, though this isn't very obvious from the documentation. Instead, you should use an OpenLayers.Feature.Vector with an externalGraphic set to some image source in its style. This notion has been carried over to OpenLayers 3, so there is no marker class and it is done as in the example you cited.

    Q2. The ol.source.Vector takes an array of features, note the line, features: [iconFeature], so you would create an array of icon features and add features to that, eg:

    var iconFeatures=[];
    
    var iconFeature = new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.transform([-72.0704, 46.678], 'EPSG:4326',     
      'EPSG:3857')),
      name: 'Null Island',
      population: 4000,
      rainfall: 500
    });
    
    var iconFeature1 = new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.transform([-73.1234, 45.678], 'EPSG:4326',     
      'EPSG:3857')),
      name: 'Null Island Two',
      population: 4001,
      rainfall: 501
    });
    
    iconFeatures.push(iconFeature);
    iconFeatures.push(iconFeature1);
    
    var vectorSource = new ol.source.Vector({
      features: iconFeatures //add an array of features
    });
    
    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: 'data/icon.png'
      }))
    });
    
    
    var vectorLayer = new ol.layer.Vector({
      source: vectorSource,
      style: iconStyle
    });
    

    Obviously, this could be more elegantly handled by putting all of the ol.Feature creation inside a loop based on some data source, but I hope this demonstrates the approach. Note, also, that you can apply a style to the ol.layer.Vector so that it will be applied to all features being added to the layer, rather than having to set the style on individual features, assuming you want them to be the same, obviously.

    EDIT: That answer doesn't seem to work. Here is an updated fiddle that works by adding the features (icons) to the empty vector source in a loop, using vectorSource.addFeature and then adds the whole lot to the layer vector afterwards. I will wait and see if this works for you, before updating my original answer.

    0 讨论(0)
提交回复
热议问题