继续我想要创建的地图导航应用程序,我遇到了以下错误:
我解析OSM磁贴并获得所有兴趣点的列表。这很成功。然后,我想在Leaflet中放置一个标记,使用一些自定义图标。上述部分的代码如下
首先包括:
<link rel="stylesheet"href="./includes/leaflet.css" />
<link rel="stylesheet" href="./includes/style.css" type="text/css">
<script src="http://cdn.leafletjs.com/leaflet-0.5/leaflet.js"></script>
<script src="./includes/jquery.js"></script>
<script src="./includes/geo.js" type="text/javascript"></script>
<script src="./includes/gears_init.js" type="text/javascript"></script>
<script type="text/javascript">
对于找到的导航点,我有一个这样的自定义图标:
if($.inArray(EleID, MarkerArray)==-1)
{
if(LocType!="")
{
var markerLocation = new L.LatLng(EleLat,EleLon);
var Icon = new CustomIcon("./images/"+LocType+".png");
var marker = new L.Marker(markerLocation,{icon : Icon});
if(EleText!="")
{
marker.bindPopup(EleText);
}
Layergroup.addLayer(marker);
MarkerArray.push(EleID);
}
}
这些图标位于./image/目录中,其类型为图标名称(hotel.png中的p.ex.酒店)
最后,我在HTML文档的正文部分创建地图
var map = new L.Map('map');
var cloudmadeUrl = 'http://a.tile.openstreetmap.org/{z}/{x}/{y}.png',
cloudmadeAttrib = 'Map data CC-BY-SA & ODbL 2012 OpenStreetMap contributors, Data via <a href="http://www.overpass-api.de/">Overpass API</a>',
cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttrib});
var CustomIcon = L.Icon.extend({
iconUrl: './images/hotel.png',
shadowUrl: './images/shadow.png',
iconSize: new L.Point(32, 32),
opacity: 0.5,
//shadowSize: new L.Point(68, 95),
iconAnchor: new L.Point(16, 16),
popupAnchor: new L.Point(0, -18)
});
虽然这对我使用的旧传单脚本效果很好,但它不适用于我正在处理的更新版本给我这个错误:
未捕获错误:iconUrl未在图标选项中设置(请参阅文档)。 leaflet.js:6
我不确定在传单中设置IconURL的位置,是否有人知道我做错了什么?
答案 0 :(得分:4)
通过扩展L.Icon.extend()
定义CustomIcon时,必须将选项包装在选项对象中。像这样:
var CustomIcon = L.Icon.extend({
options: {
iconUrl: './images/hotel.png',
shadowUrl: './images/shadow.png',
iconSize: new L.Point(32, 32),
opacity: 0.5,
//shadowSize: new L.Point(68, 95),
iconAnchor: new L.Point(16, 16),
popupAnchor: new L.Point(0, -18)
}
});