How can I use an SVG image as a map marker in OpenLayers-3?

前端 未结 6 1364
醉话见心
醉话见心 2020-12-31 13:57

I am trying to create map \"pin-drops\" (ie. map markers) in OpenLayers-3 (OL3) using SVG images.

Currently, I am using PNG images as the pindrops that reference the

6条回答
  •  一整个雨季
    2020-12-31 14:43

    Building upon SoonDead's answer, I had to come up with a way to add the width and height to the svg without touching the source. Using angular, this is sort of what I did:

    $http.get('path/to/image.svg').then(function (response) {
      // create element
      var svgEl = angular.element(response.data);
    
      // set width and height
      svgEl.attr('width', '50px');
      svgEl.attr('height', '50px');
    
      // base64 encode
      var base64Svg = btoa(unescape(encodeURIComponent(svgEl[0].outerHTML)));
    
      // create the style
      var style = new ol.style.Style({
        image: new ol.style.Icon({
          src: 'data:image/svg+xml;base64,'+base64Svg,
          imgSize: [50, 50],
          size: [50, 50],
        })
      });
    
     // apply the style
     feature.setStyle(style);
    });
    

    It's a little verbose, but it seems to do the job.

提交回复
热议问题