更改html中的自定义传单标记
change custom Leaflet markers in html
我是网络地图和传单开发的初学者。。。我发现了一个简单但有用的代码,我想知道如何用mylocal.png(或.svg)交换下面HTML代码中的所有传单标记。提前感谢您的反馈!!!祝所有度过美好的一天
<!doctype html>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css">
<style>
html, body, #map { height: 100%; margin: 0; }
</style>
<body>
<div id="map"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<script src="https://cdn.rawgit.com/tyrasd/osmtogeojson/2.2.5/osmtogeojson.js"></script>
<script>
var api = 'http://overpass-api.de/api/interpreter';
var query = 'area["place"="city"]["name"="Cluj-Napoca"];node[amenity=cafe](area);out;';
var map = L.map('map');
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
$.get(api, {data: query}, function(resp) {
var geojson = osmtogeojson(resp);
var layer = L.geoJson(geojson).addTo(map);
map.fitBounds(layer.getBounds());
});
</script>
默认情况下,使用L.GeoJSON
时,GeoJSON数据中的每个点都会变成默认的L.Marker
。您可以使用L.GeoJSON
的pointToLayer
选项返回带有使用您的图像的L.Icon
的自定义L.Marker
。
new L.GeoJSON(data {
pointToLayer: function (feature, latlng) {
return new L.Marker(latlng, {
icon: new L.Icon({
iconUrl: 'leaf-green.png',
shadowUrl: 'leaf-shadow.png',
iconSize: [38, 95], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
})
});
}
}).addTo(map);
- L.GeoJSON教程:http://leafletjs.com/examples/geojson.html
- 自定义图标教程:http://leafletjs.com/examples/custom-icons.html
试试这个:
var greenIcon = L.icon({
iconUrl: 'leaf-green.png', // url to your custome icon
iconSize: [38, 95], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
然后把这个放在地图上初始化
L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map);
就是这样。
相关文章:
- 联系表单代码自定义
- 单击自定义编辑按钮时,X可编辑切换编辑
- 使用 jQuery 和 asp.net Web 表单进行自定义确认
- 如何在HTML中的li项目上创建右键单击自定义菜单
- Jqgrid 单击自定义编辑按钮打开另一个页面
- 如何将参数传递给在表单标记外部定义的 javascript 函数
- 静音流播放器单击自定义按钮
- 允许在标记自定义动态图章后编辑表单字段
- 关注引导浮动模态表单上自定义文本之后的文本区域
- 带有表单的自定义URL
- Dropzone.js:可以't单击自定义预览
- 在tinyMCE问题中单击自定义按钮后保留选择
- 单击自定义复选框不会运行关联的onClick功能
- 在带有嵌入表单的自定义指令中使用ngModel,并进行有效的验证
- OpenLayers标记自定义文本/标签
- HTML5表单验证自定义
- CKEditor和Wiki标记(自定义数据处理器?)
- 谷歌地图标记自定义编号
- 使用jQuery表单验证自定义Ajax请求
- 添加自定义标记自定义谷歌地图