在图像的url中连接document.location.origin

时间:2017-04-26 21:28:40

标签: javascript leaflet

我需要根据下面的代码在图片的网址中连接document.location.origin。我已经用几种方式尝试了它,我无法达到任何人都可以帮助我的语法。

我需要传递src:src= document.location.origin/sin/img/blue.jpg

这是一个带有layerGroup的传单地图,其中每个都有一个图像接下来,我需要动态传递图像的url所以当它在localhost或生产服务器上时我不必交换src

var mbAttr = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetmapConsultaView</a>',
        mbUrl =  'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
    var osm   = L.tileLayer(mbUrl, {attribution: mbAttr}),
        smf  = L.tileLayer(mbUrl, {attribution: mbAttr});

    var mapConsultaView = L.map('mapConsultaView', {
        center: [-30.038037, -51.199163],
        zoom: 14,
        layer:[osm]
    });

    var baseLayers = {
        "OSM": osm,
        "SMF": smf
    };
var overlaymap = {
            "<img src='http://localhost:8080/sin/img/blue.jpg' width:'27' height:'12'> Projetados": projetadoLayer,
            "<img src='http://localhost:8080/sin/img/green.jpg' width:'27' height:'12' > Instalado": instaladoLayer,
            "<img src='http://localhost:8080/sin/img/grey.jpg' width:'27' height:'12' > Retirado": retiradoLayer,
            "<img src='http://localhost:8080/sin/img/red.jpg' width:'27' height:'12' > Desativado": desativadoLayer,
            "<img src='http://localhost:8080/sin/img/black.jpg' width:'27' height:'12'> Cancelado": canceladoLayer

    };



    //var sinalizacoesLayer = L.geoJSON(geojsonFeature).addTo(mapConsultaView); 
    L.control.layers(baseLayers,overlaymap,{collapsed:false}).addTo(mapConsultaView);
    baseLayers["OSM"].addTo(mapConsultaView);

2 个答案:

答案 0 :(得分:0)

var src = document.location.origin + '/sin/img/blue.jpg'

答案 1 :(得分:-1)

您可以将Object转换为String并查找所有匹配项并使用 str javaScript函数更改它们,或者您可以遍历对象并逐个更改它们。

var overlaymap = {
                "<img src='http://localhost:8080/sin/img/blue.jpg' width:'27' height:'12'> Projetados": "projetadoLayer",
                "<img src='http://localhost:8080/sin/img/green.jpg' width:'27' height:'12' > Instalado":"instaladoLayer",
                "<img src='http://localhost:8080/sin/img/grey.jpg' width:'27' height:'12' > Retirado":"retiradoLayer",
                "<img src='http://localhost:8080/sin/img/red.jpg' width:'27' height:'12' > Desativado":"desativadoLayer",
                "<img src='http://localhost:8080/sin/img/black.jpg' width:'27' height:'12'> Cancelado": "canceladoLayer"

};

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
}

var stringOverlaymap = JSON.stringify(overlaymap);


stringOverlaymap = replaceAll(stringOverlaymap, "http://localhost:8080", "document.location.origin");



overlaymap = JSON.parse(stringOverlaymap);


console.log(overlaymap);