首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传单(交互式地图)-带有Spring框架自定义图标的标记(静态内容)

问传单(交互式地图)-带有Spring框架自定义图标的标记(静态内容)
EN

Stack Overflow用户
提问于 2016-07-02 20:07:50
回答 1查看 1.2K关注 0票数 1

我在Spring框架应用程序中使用传单。我想使用自定义图标使用costum图像为不同的图标。

这是我的项目的层次结构:

代码语言:javascript
复制
webaapp
   --- core
-------- css
-------- js
-----------images (leafletjs images)

这里的代码

代码语言:javascript
复制
 <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/core/css/leaflet.css" />
    <title>leafletjs</title>
    </head>
    <body>


THIS IMAGE IS OK, I CAN SEE IT:
        <img src="${pageContext.request.contextPath}/resources/core/images/marker-icon-2x-blue.png"/>

        <div id="mapid" style="width: 800px; height: 600px"></div>


        <script src="${pageContext.request.contextPath}/resources/core/js/leaflet.js"></script>
        <script>

            var mymap = L.map('mapid').setView([51.505, -0.09], 13);

            L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
                maxZoom: 18,
                attribution:
                    'osm',
                id: 'mapbox.streets'
            }).addTo(mymap);

            var greenIcon  = new LeafIcon({iconUrl: '${pageContext.request.contextPath}/resources/core/js/images/marker-icon-2x-green.png'}),
                redIcon    = new LeafIcon({iconUrl: '${pageContext.request.contextPath}/resources/core/js/images/marker-icon-2x-red.png'}),
                blueIcon   = new LeafIcon({iconUrl: '${pageContext.request.contextPath}/resources/core/js/images/marker-icon-2x-blue.png'}),
                yellowIcon = new LeafIcon({iconUrl: '${pageContext.request.contextPath}/resources/core/js/images/marker-icon-2x-yellow.png'});



            L.marker([51.5, -0.09], {icon: greenIcon}).addTo(mymap)
                .bindPopup(" ").openPopup();

            L.marker([51.49, -0.091], {icon: greenIcon}).addTo(mymap)
            .bindPopup(" ").openPopup();

            L.marker([51.48, -0.092], {icon: greenIcon}).addTo(mymap)
            .bindPopup("").openPopup();

            L.marker([51.47, -0.098], {icon: greenIcon}).addTo(mymap)
            .bindPopup(" ").openPopup();

            var popup = L.popup();


        </script>   
    </body>
    </html>

如果我不对图像进行缓冲,我就会看到默认的标记图标就在这里,它会完美地显示出来:

代码语言:javascript
复制
http://localhost:8080/myApp-1.0-SNAPSHOT/resources/core/js/images/marker-icon-2x.png

但是对于定制的那些,我尝试了几件没有成功的事情:

代码语言:javascript
复制
new LeafIcon({iconUrl: '${pageContext.request.contextPath}/resources/core/js/images/marker-icon-2x-green.png'}),

new LeafIcon({iconUrl: 'images/marker-icon-2x-green.png'}),

new LeafIcon({iconUrl: '/images/marker-icon-2x-green.png'}),

new LeafIcon({iconUrl: '/resources/core/js/images/marker-icon-2x-green.png'}),

new LeafIcon({iconUrl: 'resources/core/js/images/marker-icon-2x-green.png'}),

在脚本的内部内容中添加一个警报(‘${pageContext.request.contextPath}’)以查看它的解析,消息是我的应用程序的contect名称:

我也试过

代码语言:javascript
复制
<spring:url value="/resources/core/js/images/marker-icon-2x-green.png" var="myImage" />
 var greenIcon  = new LeafIcon({iconUrl:'${myImage}'})

它也不起作用,即使在JSP中也是如此。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-05 09:28:27

编辑问题8-12之后的:

好的,如果我理解正确的话,您在与传单默认图标相同的images文件夹中添加了自定义图标图像?

在这种情况下,您可以利用传单默认图标图像自动路径解析L.Icon.Default.imagePath来构建您自己的路径。

我想是这样的:

代码语言:javascript
复制
new LeafIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-green.png'});

你在哪里定义你的LeafIcon

在脚本的内部内容中,'${pageContext.request.contextPath}'会解析什么?

你试过'resources/core/images/marker-icon-2x-green.png'了吗?(注意没有前导斜杠)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38164061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档