2016-09-29 37 views
1

如何在symfony 2.8中使用Openlayers 3脚本将图像包含在树枝中?如何在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 
    }) 
}); 

回答

0

嫩枝无关与嵌入openlayers。因为它是一个JavaScript库。

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

此外,openlayers document是相当自我解释。

+0

如果我使用简单的自己的脚本(和自己的简单MVC),我可以包含图像。我的意思是我明白如何在OpenLayers中实现src,但在Symfony中这不起作用。如何在Symfony 2.8中做到这一点? – olga

+0

我也可以在Symfony项目视图的树枝模板中包含OpenLayer 3 javascript的图像。但我无法将脚本本身的图像分配给src。 – olga

+0

我的意思是我可以包含图像到OpenLayer 3如果我在PHP脚本,或自己简单的MVC。不过,我不能在Symfony 2项目中做到这一点。但是我可以在任何Symfony 2项目模板中插入图像,但不在模板中的OpenLayer 3脚本中。我试图将图像包含在包含OpenLayer脚本的相同模板中。使用以上三种方法中的任何一种将图像包含在模板中。但是,如果我尝试将其包含到OpenLayer对象的脚本中 - 它不起作用。 – olga