I am currently placing markers with popups on a leaflet maps as follows:
L.marker([33.767675, -84.537291], {icon:orangeIcon}).addTo(map).bindPopup("a bunch of dynamic html content for the popup window");
I have many markers, with lots of popup content, so in order to speed up my map rendering/page loading I would like to populate the actual popup with URL-based content, but only when that marker is clicked.
Any ideas on how this can be done?
I did find this example (via https://github.com/Leaflet/Leaflet/issues/947) , but my popup just stays at "loading..." and never seems to load the url:
var marker = L.marker([33.767675, -84.537291]).addTo(map);
marker.bindPopup("Loading...");
marker.on('click', function(e) {
var popup = e.target.getPopup();
var url="http://www.someurl.com/file.html";
$.get(url).done(function(data) {
popup.setContent(data);
popup.update();
});
});
In thinking maybe the .get method should be .ajax , I tried this...but still no dice:
var marker = L.marker([33.767675, -84.537291]).addTo(map);
marker.bindPopup("Loading...");
function onMapClick(e) {
var popup = e.target.getPopup();
var url="http://www.r-stop.com";
$.ajax({
url:"http://www.r-stop.com/index.html"
}).done(function( data ) {
popup.setContent( data );
popup.update();
});
};
marker.on('click', onMapClick );
So...EDIT.... the below code works, but returns the .fail function. Seems there is something up with my $.ajax request. The popup is populated with 'FAIL: [object OBJECT]'
var marker = L.marker([33.767675, -84.537291]).addTo(map);
marker.bindPopup("Loading...");
function onMapClick(e) {
var popup = e.target.getPopup();
$.ajax({
url: "myfile.html",
})
.done(function( data ) {
popup.setContent( data );
popup.update();
})
.fail(function( data ) {
popup.setContent( 'FAIL: ' + data );
popup.update();
});
};
marker.on('click', onMapClick );