传单geojson不会显示标记
leaflet geojson wont show marker
我正在尝试在我的应用程序中用带有传单的标记可视化一个地点。
为此,我正在构建一个正在测试网站上运行的功能 (https://embed.plnkr.co/plunk/iiGl5i)
[{"type":"Feature","geometry":{"type":"Point","coordinates":[30.0,34.0]},"properties":{"divesite_id":8,"name":"Cyprus, Larnaca, Zenobia","show_on_map":true}}]
如果我使用 L.marker 和 addTo(map),与 leaflet 的集成有效,但它不适用于 geojson。
application.html.erb
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<style>
#map {height: 400px}
</style>
_form.html.erb
<div id=map></div>
<script>
var lng = '<%= @divesite.longitude %>';
var lat = '<%= @divesite.latitude %>';
var popup = '<b><%= @divesite.name %></b><br><%= @divesite.description %>';
var geojson = '<%= @geojson %>';
var map = L.map('map').setView([lat, lng], 5);
L.tileLayer('https://api.maptiler.com/maps/topo/{z}/{x}/{y}.png?key=WuMlitiPNaB5uEkyEiZc').addTo(map);
// var marker1 = L.marker([lat, lng]).addTo(map);
// marker1.bindPopup(popup);
function onEachFeature(feature, layer) {
var popupContent = "<p> "+feature.properties.name + "</p>";
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
};
L.geoJson(geojson,{onEachFeature: onEachFeature}).addTo(map);
</script>
我尝试了几种我找到的编码方法,但找不到让它工作。请帮助:)
你的geojson是字符串,你要解析成对象
var hospitals = JSON.parse(geojson):
L.geoJson(hospitals ,{onEachFeature: onEachFeature}).addTo(map);
更新
要解码 html 你可以使用这个:(感谢@ghybs)
function htmlDecode(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
var hospitals = JSON.parse(htmlDecode(geojson)):
L.geoJson(hospitals ,{onEachFeature: onEachFeature}).addTo(map);
所以,这是有效的:)
function onEachFeature(feature, layer) {
var popupContent = "<p> "+feature.properties.name + "</p>";
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
};
function htmlDecode(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
};
var sites = JSON.parse(htmlDecode('<%= @geojson %>'));
L.geoJson(sites ,{onEachFeature: onEachFeature}).addTo(map);
我刚刚修正了一些拼写错误 - 感谢@Falke Design 和@ghybs 的帮助
我正在尝试在我的应用程序中用带有传单的标记可视化一个地点。 为此,我正在构建一个正在测试网站上运行的功能 (https://embed.plnkr.co/plunk/iiGl5i)
[{"type":"Feature","geometry":{"type":"Point","coordinates":[30.0,34.0]},"properties":{"divesite_id":8,"name":"Cyprus, Larnaca, Zenobia","show_on_map":true}}]
如果我使用 L.marker 和 addTo(map),与 leaflet 的集成有效,但它不适用于 geojson。
application.html.erb
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<style>
#map {height: 400px}
</style>
_form.html.erb
<div id=map></div>
<script>
var lng = '<%= @divesite.longitude %>';
var lat = '<%= @divesite.latitude %>';
var popup = '<b><%= @divesite.name %></b><br><%= @divesite.description %>';
var geojson = '<%= @geojson %>';
var map = L.map('map').setView([lat, lng], 5);
L.tileLayer('https://api.maptiler.com/maps/topo/{z}/{x}/{y}.png?key=WuMlitiPNaB5uEkyEiZc').addTo(map);
// var marker1 = L.marker([lat, lng]).addTo(map);
// marker1.bindPopup(popup);
function onEachFeature(feature, layer) {
var popupContent = "<p> "+feature.properties.name + "</p>";
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
};
L.geoJson(geojson,{onEachFeature: onEachFeature}).addTo(map);
</script>
我尝试了几种我找到的编码方法,但找不到让它工作。请帮助:)
你的geojson是字符串,你要解析成对象
var hospitals = JSON.parse(geojson):
L.geoJson(hospitals ,{onEachFeature: onEachFeature}).addTo(map);
更新
要解码 html 你可以使用这个:(感谢@ghybs)
function htmlDecode(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
var hospitals = JSON.parse(htmlDecode(geojson)):
L.geoJson(hospitals ,{onEachFeature: onEachFeature}).addTo(map);
所以,这是有效的:)
function onEachFeature(feature, layer) {
var popupContent = "<p> "+feature.properties.name + "</p>";
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
};
function htmlDecode(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
};
var sites = JSON.parse(htmlDecode('<%= @geojson %>'));
L.geoJson(sites ,{onEachFeature: onEachFeature}).addTo(map);
我刚刚修正了一些拼写错误 - 感谢@Falke Design 和@ghybs 的帮助