在html中更改自定义Leaflet标记

时间:2015-12-10 14:01:31

标签: javascript html leaflet openstreetmap overpass-api

我是网络地图和传单开发的初学者...我找到了一个简单但有用的代码,我想知道如何用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: '&copy; <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>

2 个答案:

答案 0 :(得分:2)

默认情况下,当使用L.GeoJSON时,GeoJSON数据中的每个点都会变为默认L.Marker。您可以使用pointToLayer的{​​{1}}选项返回自定义L.GeoJSON,其L.Marker使用您的图片。

L.Icon

答案 1 :(得分:0)

试试这个:

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
});

然后把它放到地图initilize

L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map);

是的。