如何在地图中显示自定义标记
How to display custom markers in Map
您好,我需要在地图中显示自定义标记,但它无法在我的地图上显示,请帮助我如何显示自定义标记。
这是我的json对象:
{
"features": [
{
"geometry": {
"coordinates": [
[-89.39982622861862, 43.06710857435938 ],
[-89.39982622861862, 43.06710857435938 ]
],
"type": "Point"
},
"id": "ci7gsmklj004ahym0qpgca8j7",
"properties": {
"icon": {
"options": {
"className": "",
"iconUrl": "/hb/assets/leaflet/images/d.png",
"iconSize": [ 64, 64],
"iconAnchor": [ 32, 32],
"popupAnchor": [ 0, -24 ]
},
"_initHooksCalled": true
},
"text": "Hari",
"title": "Hunt Area #1"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
这是我的代码:
var layer = new L.tileLayer('http://{s}.tiles.mapbox.com/v4/zittelevan.lgjj093b/{z}/{x}/{y}.png?access_token={token}', {
attribution: 'Attribute data here'
maxZoom: 18,
token: "Token Here"
});
var map = new L.map('map', {
center:[43.10361493125458,89.52398300170898],
zoom: 18,
layers: [layer],
});
L.geoJson(features).addTo(map);
我关注了不同的网站,但无法找到正确的解决方案,所以我请求你们帮助我如何在地图上显示自定义标记。
您需要使用 L.geoJson
的 pointToLayer
(http://leafletjs.com/reference.html#geojson-pointtolayer) 选项。 Leafet 无法通过检查 GeoJSON 功能的属性散列来确定自定义标记设置,您需要在下面进行设置 yourself.Code。
var myIcon = L.icon({
"className": "",
"iconUrl": "/hb/assets/leaflet/images/d.png",
"iconSize": [ 64, 64],
"iconAnchor": [ 32, 32],
"popupAnchor": [ 0, -24 ]
});
geojsonOptions = {
pointToLayer: function(feature,latLng) {
return L.marker(latLng,{icon:myIcon})
}
}
L.geoJson(features).addTo(map);
我是这样做的,我得到了答案
var myIcon = L.icon({
"className": "",
"iconUrl": "/hb/assets/leaflet/images/d.png",
"iconSize": [ 64, 64],
"iconAnchor": [ 32, 32],
"popupAnchor": [ 0, -24 ]
});
L.marker([44.86475133784528,-88.98447811603546],{icon:myIcon }).addTo(map);
您好,我需要在地图中显示自定义标记,但它无法在我的地图上显示,请帮助我如何显示自定义标记。
这是我的json对象:
{
"features": [
{
"geometry": {
"coordinates": [
[-89.39982622861862, 43.06710857435938 ],
[-89.39982622861862, 43.06710857435938 ]
],
"type": "Point"
},
"id": "ci7gsmklj004ahym0qpgca8j7",
"properties": {
"icon": {
"options": {
"className": "",
"iconUrl": "/hb/assets/leaflet/images/d.png",
"iconSize": [ 64, 64],
"iconAnchor": [ 32, 32],
"popupAnchor": [ 0, -24 ]
},
"_initHooksCalled": true
},
"text": "Hari",
"title": "Hunt Area #1"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
这是我的代码:
var layer = new L.tileLayer('http://{s}.tiles.mapbox.com/v4/zittelevan.lgjj093b/{z}/{x}/{y}.png?access_token={token}', {
attribution: 'Attribute data here'
maxZoom: 18,
token: "Token Here"
});
var map = new L.map('map', {
center:[43.10361493125458,89.52398300170898],
zoom: 18,
layers: [layer],
});
L.geoJson(features).addTo(map);
我关注了不同的网站,但无法找到正确的解决方案,所以我请求你们帮助我如何在地图上显示自定义标记。
您需要使用 L.geoJson
的 pointToLayer
(http://leafletjs.com/reference.html#geojson-pointtolayer) 选项。 Leafet 无法通过检查 GeoJSON 功能的属性散列来确定自定义标记设置,您需要在下面进行设置 yourself.Code。
var myIcon = L.icon({
"className": "",
"iconUrl": "/hb/assets/leaflet/images/d.png",
"iconSize": [ 64, 64],
"iconAnchor": [ 32, 32],
"popupAnchor": [ 0, -24 ]
});
geojsonOptions = {
pointToLayer: function(feature,latLng) {
return L.marker(latLng,{icon:myIcon})
}
}
L.geoJson(features).addTo(map);
我是这样做的,我得到了答案
var myIcon = L.icon({
"className": "",
"iconUrl": "/hb/assets/leaflet/images/d.png",
"iconSize": [ 64, 64],
"iconAnchor": [ 32, 32],
"popupAnchor": [ 0, -24 ]
});
L.marker([44.86475133784528,-88.98447811603546],{icon:myIcon }).addTo(map);