【问题标题】:How to include image to twig using Openlayers 3 script in symfony 2.8?如何在 symfony 2.8 中使用 Openlayers 3 脚本将图像包含到树枝中?
【发布时间】:2017-01-12 06:43:08
【问题描述】:

如何在 symfony 2.8 中使用 Openlayers 3 脚本将图像包含到树枝中?

也不是资产,也不是从根目录引用,也不是从当前目录引用。

      src: "{{ asset('bundles/meeting/images/google-map-pointer-grey.svg')  }}"  // does not work
      src: "{{ asset('/bundles/meeting/images/google-map-pointer-grey.svg')  }}"  // does not work
       src: "/bundles/meeting/images/google-map-pointer-grey.svg"  // from root directory also does no work
       src: "../../../images/google-map-pointer-grey.svg"  // referencing from the current directory also does no work

代码来自示例: http://openlayers.org/en/v3.2.1/examples/drag-features.html

我只是使用了不同的地图,它工作并显示,只使用了一个特征 - 具有不同坐标的点,这不起作用,它没有显示在地图上。

//树枝模板

<script>

  window.onload = function() {
  var lat = document.getElementById('edit_form.latitude').value; 
  var lon = document.getElementById('edit_form.longitude').value; 

var pointM = [ parseFloat(lon), parseFloat(lat) ];
var pointMWebMercator = ol.proj.fromLonLat( pointM, 'EPSG:3857' );
console.log( ' pointMWebMercator m= '+ pointMWebMercator);

var pointFeature = new ol.Feature(new ol.geom.Point(pointMWebMercator));


var map = new ol.Map({
  interactions: ol.interaction.defaults().extend([new app.Drag()]),
  target: 'Openmap',  // The DOM element that will contains the map
  renderer: 'canvas', // Force the renderer to be used
  size: [200, 200],
  layers: [
    new ol.layer.Tile({  source: new ol.source.OSM()  }),
    new ol.layer.Vector({
      source: new ol.source.Vector({
        features: [pointFeature]
      }),
      style: new ol.style.Style({
        image: new ol.style.Icon( ({
            // @type {olx.style.IconOptions} 
          anchor: [0.5, 46],
          anchorXUnits: 'fraction',
          anchorYUnits: 'pixels',
          opacity: 0.95,
          src: "{{ asset('bundles/meeting/images/google-map-pointer-grey.svg')  }}"  // does not work
         // src: "{{ asset('/bundles/meeting/images/google-map-pointer-grey.svg')  }}"  // does not work
         //  src: "/bundles/meeting/images/google-map-pointer-grey.svg"  // from root directory also does no work
         //  src: "../../../images/google-map-pointer-grey.svg"  // referencing from the current directory also does no work

}) ),

  //  A leading slash tells the browser to start at the root directory.
//// i can get the root directory from//print_r("SERVER[DOCUMENT_ROOT]".$_SERVER['DOCUMENT_ROOT']);
//// for symfony it is project/web
  //  If you don't have the leading slash, you're referencing from the current directory.
 //   If you add two dots before the leading slash, it means you're referencing the parent of the current directory.



        stroke: new ol.style.Stroke({
          width: 3,
          color: [255, 0, 0, 1]
        }),
        fill: new ol.style.Fill({
          color: [0, 0, 255, 0.6]
        })
      })
    })
  ],
  view: new ol.View({
    center: pointMWebMercator,
    zoom: 14
  })
});

【问题讨论】:

    标签: symfony anchor openlayers-3 src


    【解决方案1】:

    Twig 与嵌入 openlayers 无关。因为它是一个 javascript 库。

    Twig 在服务器端执行并生成HTMLOpenLayer 脚本应该在客户端执行。

    另外,openlayers document 非常不言自明。

    【讨论】:

    • 如果我使用简单的自己的脚本(和自己的简单 MVC),我可以包含图像。我的意思是我了解如何在 OpenLayers 中实现 src,但在 Symfony 中这不起作用。如何在 Symfony 2.8 中做到这一点?
    • 我还可以将图像包含在拥有 OpenLayer 3 javascript 的 Symfony 项目视图树枝模板的主体中。但我无法在脚本本身中将图像分配给 src。
    • 我的意思是,如果我在 php 脚本中制作图像,或者拥有简单的 MVC,我可以将图像包含到 OpenLayer 3 中。尽管如此,我不能在 Symfony 2 项目中做到这一点。但是我可以在任何 Symfony 2 项目模板中包含图像,但不能在模板中的 OpenLayer 3 脚本中。我试图将图像包含在包含 OpenLayer 脚本的同一模板中。使用所描述的三种方式中的任何一种将图像包含到模板中。但是,如果我尝试使用 OpenLayer 对象将其包含在脚本中 - 它不起作用。
    • 给我时间再仔细检查一次 - 将在 8 小时内回信
    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多