3

私は Web マップとリーフレットの開発の初心者です... シンプルだが便利なコードを見つけました。以下の HTML コード内のすべてのリーフレット マーカーを mylocal.png (または .svg) と交換する方法を知りたいです。フィードバックをお寄せいただきありがとうございます!!! 良い一日を

<!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>
4

2 に答える 2

2

デフォルトでは、GeoJSON データ内のすべてのポイントを使用L.GeoJSONすると、デフォルトに変わりますL.Marker。のpointToLayerオプションを使用して、イメージを使用する でカスタマイズされたL.GeoJSONを返すことができます。L.MarkerL.Icon

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);
于 2015-12-10T14:24:32.203 に答える
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
});

次に、これをマップの初期化に入れます

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

それでおしまい。

于 2015-12-10T14:14:17.753 に答える