Reputation: 21
I've recently been offered a small part time job managing a website, the job description involves use of HTML but they want me to work with a leaflet created map (published via RPub & Shiny but probably created with Java). Specifically embedding it into the website and adding images to the markers of the sites which are mapped. Now, I've been digging around in the HTML code for the resulting map and I can't see a way to add images to the map using the existing HTML. Perhaps my HTML skills aren't up to scratch here. However, having looked around the web for information on adding images to leaflet markers suggests I may need to edit this in java, which I don't have the original code for. Do I need the original code to be able to do this?
The map in question is hosted here:
http://rpubs.com/roycostilla/placesofworship
I'm not entirely sure if I'm missing something obvious, or if what they want from me isn't doable without the map's Java code. I appreciate my post isn't as detailed as it could be, but I'm a little bit confused and overwhelmed myself as I have only small amounts of HTML experience and none in Java.
Upvotes: 0
Views: 369
Reputation: 53280
Not exactly sure about your Java thing.
If my understanding is correct, the Leaflet map you want to work with has been created as a Shiny App from R language.
Leaflet is a JavaScript library. The version of that library used by Shiny appears to be 0.7.3 (quite old now, but still works).
There is quite some good API documentation even for that version. But you need to write JavaScript code to work with it.
Now if I understand correctly, you would like to modify the way that map is currently done, like changing the markers. Then to include it in an HTML page.
You have several options here:
<iframe>
in your page.Here is a demonstration for point 3:
var group = L.featureGroup();
$(document).ready(function () {
var mapContainer = $("#htmlwidget-98ae620727ee15448353")[0];
var map = mapContainer["htmlwidget_data_init_result"];
map.eachLayer(function(layer) {
// Filter only for Circle Markers, as the map looks to use only those for data.
if (layer instanceof L.CircleMarker) {
layer.addTo(group);
// Embed the popup data into the layer feature properties, so that it is recorded in the GeoJSON output.
layer.feature = layer.feature || {}; // Initialize the layer feature, in case it does not exist yet.
layer.feature.type = "Feature";
layer.feature.properties = layer.feature.properties || {};
layer.feature.properties.data = layer._popup.getContent();
layer.feature.properties.color = layer.options.color;
}
});
console.log(group.toGeoJSON());
});
Live demo: https://plnkr.co/edit/oWbewb53CLZxmfVj8WbN
Once you have your data as GeoJSON, it is easy to create a new Leaflet map in JavaScript. See for example the Leaflet GeoJSON tutorial.
Note: as your map has more than 2,000 points, using markers with icons (instead of Circle Markers) will considerably slow down browsers. You should either stick on using Circle Markers (ideally painting them on a Canvas instead of SVG), or use a clustering plugin, e.g. Leaflet.markercluster.
Good luck!
Upvotes: 4