Style popups differently depending on the clicked layer using Mapbox GL JS - mapbox

I am currently trying to give my popups different background based on the layer they are found in (i.e. I have a layer called Indigenous Sites where I would like the background of the popup to be different from the other layers).
I have tried giving the popup a className but I am not sure how to call it properly in the CSS.
Below is a sample of the popup script in my html document (script for 2 different layers popups):
// popup for the Other European Sites layer
map.on('click', 'Other European Sites', function (e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML('<h2>' + 'European Site' + '</h2>' +
'<p>' + e.features[0].properties.placeName + '</p>' +
'<h2>' + 'Story' + '</h2>' +
'<p>' + e.features[0].properties.Story + '</p>' +
'<h2>' + 'Latitude' + '</h2>' +
'<p>' + e.features[0].properties.latitude + '</p>' +
'<h2>' + 'Longitude' + '</h2>' +
'<p>' + e.features[0].properties.longitude + '</p>')
.addTo(map);
});
// popup for the Other Indigenous Sites layer
map.on('click', 'Other Indigenous Sites', function (e) {
new mapboxgl.Popup({className: 'popupCustom'})
.setLngLat(e.lngLat)
.setHTML('<h2>' + 'Indigenous Site' + '</h2>' +
'<p>' + e.features[0].properties.placeName + '</p>' +
'<h2>' + 'Story' + '</h2>' +
'<p>' + e.features[0].properties.Story + '</p>' +
'<h2>' + 'Latitude' + '</h2>' +
'<p>' + e.features[0].properties.latitude + '</p>' +
'<h2>' + 'Longitude' + '</h2>' +
'<p>' + e.features[0].properties.longitude + '</p>')
.addTo(map);
});
Here is my current CSS that gives all of my popups the same background:
.mapboxgl-popup-content {
overflow-y: scroll;
background-color: #000000;
}
How would I go about assigning the popups different classNames and calling them in the CSS so that I can have different backgrounds for each layer?
Thanks so much for any input!

The className is a CSS class name that will be applied to the popup container (which contains the "mapboxgl-popup-content" element). So if you want your "Other Indigenous Sites" layer to have, say, yellow popups, you could do this:
.mapboxgl-popup-content {
overflow-y: scroll;
background-color: #000000;
}
.popupCustom .mapboxgl-popup-content {
background-color: yellow;
}
Note however that this functionality was only very recently added and I don't think it has been released yet. (It's not in the published docs).

Related

Leaflet JS- Highlight on hover

I created a simple web map and now I am stacked on how to use the highlight function on hover. I tried to use the tutorial on the Leaf js site, but when I am trying to call the highlight function under geoJason variable, it is not working. Basically I want the polygon to be highlighted when hovered. The popup is working just fine.
Here is my script, without the highlight function.
ar map = L.map('map').setView([43.0982, -89.3811], 12);
var googleSat = L.tileLayer('http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',{
minZoom: 1,
maxZoom: 20,
subdomains:['mt0','mt1','mt2','mt3'],
attribution: '<a href="https://www.google.com/maps" target=_blank> Google Sattellite Map</a>' }).addTo(map);
// Adding the geoJason file and styling it.
var myStyle = {
fillColor: "#2c7fb8",
color: "#f20b0b",
weight: 1,
opacity: 1,
fillOpacity: 0.55
};
// Alder district was a geoJason file and now it saved as js file.
var geojason = L.geoJSON(alderdstricts, {
style:myStyle,
onEachFeature:districtdata,
}).addTo(map);
// Function to bind popup to the geoJason data.
function districtdata(feature, layer){
layer.bindPopup("<span class='headings'>District: </span>" + feature.properties.ALD_DIST + "<br>" +
"<span class='headings'>Representative: </span>" + feature.properties.Representa + "<br>"
+ "<span class='headings'>Contact Representative: </span>" + feature.properties.ContactRep + "<br>"
+ "<span class='headings'>District Population: </span>" + feature.properties.DistrictPo
+ "<span class='headings'></span>" + feature.properties.Image)
};
I added all of my script above except the highlight function and that is where I am stacked.
var map = L.map('map').setView([43.0982, -89.3811], 12);
var googleSat = L.tileLayer('http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',{ minZoom: 1, maxZoom: 20, subdomains:['mt0','mt1','mt2','mt3'], attribution: ' Google Sattellite Map' }).addTo(map);
// Adding the geoJason file and styling it.
var myStyle = {
fillColor: "#2c7fb8",
color: "#f20b0b",
weight: 1,
opacity: 1,
fillOpacity: 0.55
};
// Alder district was a geoJason file and now it saved as js file.
var geojason = L.geoJSON(alderdstricts, { style:myStyle, onEachFeature:districtdata,
}).addTo(map);
// Function to bind popup to the geoJason data.
function districtdata(feature, layer){
layer.bindPopup("District: " + feature.properties.ALD_DIST + "
" + "Representative: " + feature.properties.Representa + "
" + "Contact Representative: " + feature.properties.ContactRep + "
" + "District Population: " + feature.properties.DistrictPo + "" + feature.properties.Image)
layer.on('mouseover', function(e) {
e.target.setStyle({
fillOpacity: 0.8
});
});
layer.on('mouseout', function(e) {
e.target.setStyle({
fillOpacity: 0.55
});
});
};
I'm not sure what you tried, but this should work. It simply adds a "mouseover" and "mouseout" event handler for each polygon, as well as your pop-up. If this doesn't work, please let me know what errors/behaviour you get.
In general, it's best to provide details on what solutions you have already tried in your question. Also, just FYI, JSON is not a contraction of Jason, it stands for JavaScript Object Notation.

Present Time Range in Grafana's Text Panel

Is there a way to show timeRange.from and timeRange.to in Grafana's Text panel or may be in an another panel?
I tried text-panels with markdown mode:
# Time From: $__interval
with different variables but no one did work. Neither did it if I defined a variable in Settings. The interval or timestamp is not showing.
I'm currently working on an HTML solution, but wondering if there is a simplier/proper way to present the time ranges?
My HTML-solution, in case somebody faces the same issue
(got the main part from maxreb's solution from: https://github.com/grafana/grafana/issues/1909) :
<script type="text/javascript">
refreshTimeFrom();
refreshTimeTo();
angular.element('grafana-app').injector().get('timeSrv').$rootScope.$on('refresh', function(event, data) { refreshTimeFrom(); });
function refreshTimeFrom( )
{
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var ts = new Date(angular.element('grafana-app').injector().get('timeSrv').timeRange().from);
document.getElementById("timeFrom").innerHTML = days[ts.getUTCDay() ] + " "
+ ts.getUTCDate() + "." + (ts.getUTCMonth()+1) + "."
+ ts.getUTCFullYear() + " " + ts.getUTCHours() + ":"
+ ts.getUTCMinutes() + ":" + ts.getUTCSeconds();
}
function refreshTimeTo( )
{
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var ts = new Date(angular.element('grafana-app').injector().get('timeSrv').timeRange().to);
document.getElementById("timeTo").innerHTML = days[ts.getUTCDay() ] + " "
+ ts.getUTCDate() + "." + (ts.getUTCMonth()+1) + "."
+ ts.getUTCFullYear() + " " + ts.getUTCHours() + ":"
+ ts.getUTCMinutes() + ":" + ts.getUTCSeconds();
}
</script>
<font size = "3"><b> From:</b> <span id="timeFrom" /></br></font>
<font size = "3"><b> To: </b> <span id="timeTo" /> </font>

Need to stop autoplay of video in popup

I have a video in a popup module, can see it here, select the View Video link http://www.mcveaghfleming.co.nz/sandbox.html
The issue is the video plays immediately once the link is selected. I want to stop this from happening so that it only plays using the Play button control.
I have tried autoplay: false; etc but this doesn't work. Below is the script and html etc.
Would really appreciate any assistance on getting this to work better.
Thanks
<!-- video activate link -->
<span class="learnmore">VIEW VIDEO </span>
<!-- the popup video-->
<div id="vidBox">
<div id="videCont">
<video id="v1" controls>
<source src="video/mcveaghfleming-100-years.mp4" type="video/mp4">
</video>
</div>
</div>
<!-- end-->
(function ($) {
$.fn.VideoPopUp = function (options) {
var defaults = {
backgroundColor: "#000000",
opener: "video",
maxweight: "640",
pausevideo: false,
idvideo: ""
};
var patter = this.attr('id');
var settings = $.extend({}, defaults, options);
var video = document.getElementById(settings.idvideo);
function stopVideo() {
video.pause();
video.currentTime = 0;
}
$('#' + patter + '').css("display", "none");
$('#' + patter + '').append('<div id="opct"></div>');
$('#opct').css("background", settings.backgroundColor);
$('#' + patter + '').css("z-index", "100001");
$('#' + patter + '').css("position", "fixed")
$('#' + patter + '').css("top", "0");
$('#' + patter + '').css("bottom", "0");
$('#' + patter + '').css("right", "0");
$('#' + patter + '').css("left", "0");
$('#' + patter + '').css("padding", "auto");
$('#' + patter + '').css("text-align", "center");
$('#' + patter + '').css("background", "none");
$('#' + patter + '').css("vertical-align", "vertical-align");
$("#videCont").css("z-index", "100002");
$('#' + patter + '').append('<div id="closer_videopopup">⊗ </div>');
$("#" + settings.opener + "").on('click', function () {
$('#' + patter + "").show();
$('#'+settings.idvideo+'').trigger('play');
});
$("#closer_videopopup").on('click', function () {
if(settings.pausevideo==true){
$('#'+settings.idvideo+'').trigger('pause');
}else{
stopVideo();
}
$('#' + patter + "").hide();
});
return this.css({
});
};
}(jQuery));
You are telling it to play in your code. Take the following line out:
$('#'+settings.idvideo+'').trigger('play');

Bing map infobox on hover over a li not displaying

I am working with integration of bing maps into the application. When the search button is clicked after entering the zip code information, the div below will display a list of available stores and a map with pushpin on that. Showing an infobox is working when I hover over the pushpin. But my requirement is that, I have to show the particular infobox to the user, when the user hovers over the list in the left of the maps.
For example here, when I hover over the first result on the left, the corresponding infobox should show in the map. I am unable to figure out why it not working. Appreciate your help in advance. Thank you. Please find below what I have tried so far.
for (var i = 0; i < data.length; i++) {
if (storeLoc && (data[i].metadata.LocationTypeSort == "ja")) {
console.log(counter);
console.log(data);
innerTablecontent += "<tr><td><h4 class='h4-mapDetails-storeName'>" + '<div style="display: inline-block; vertical-align: middle"><svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><circle cx="12.5" cy="12.5" r="12.5"/><text x="50%" y="17" text-anchor="middle" fill="white" font-size="14" font-weight="bold">' + (+counter) + '</text></svg></div>' +
" " + "<b><span style='margin-top:-20px;display:block;margin-left:45px;'>" + data[i].metadata.LocationName + "</span></b>" + "</h4><p class='p-mapDetails'>" + data[i].metadata.AddressLine + "," + data[i].metadata.Locality + "," + data[i].metadata.AdminDistrict + " " + data[i].metadata.PostalCode + "</p>"
+ "<p class='p-mapDetails'>" + data[i].metadata.Phone + "<span><span class='index hidden'>" + i + "</span> | " + '<a style=" font-family:Helvetica Neue LT Pro Roman,sans-serif;color:#00A7CE" href="">View details</a>' + "</span></p>"+ "<span class='miles-mapDetails'>" +(data[i].metadata.__Distance* 0.6214).toFixed(2)+"mi</span></td></tr>"
locations.push(data[i].getLocation());
var pin1 = createCirclePushpin(data[i].getLocation(), 12.5, 'rgb(0, 0, 0)', 'black', 1, counter);
pin1.metadata = {
//title: counter + "." + " " + data[i].metadata.LocationName,
title: " ",
description: '<div style="display: inline-block; vertical-align: middle; margin-right:10px;"><svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><circle cx="12.5" cy="12.5" r="12.5"/><text font-weight="bold" x="50%" y="17" text-anchor="middle" fill="white" font-size="14">' + (+counter) + '</text></svg></div>' + "<span class='h4-mapDetails-storeName'>" + data[i].metadata.LocationName + "</span><p style='margin-bottom:-3px;font-family:Helvetica Neue LT Pro Roman,Helvetica,sans-serif;font-style:normal !important;color:#000;font-size:14px;'>" + data[i].metadata.AddressLine + ", " + data[i].metadata.Locality + "," + data[i].metadata.AdminDistrict + " " +
data[i].metadata.PostalCode + "<br>" + data[i].metadata.Phone + "</p>" + "<a>" + '<a style="font-size:14px;font-family:Helvetica Neue LT Pro Roman,Helvetica,sans-serif;color:#00A7CE" href="">View details</a>' + "</a>"
};
Microsoft.Maps.Events.addHandler(pin1, 'mouseover', pushpinClicked);
map.entities.push(pin1);
counter++;
}
Function to call when the list is hovered:
$("#mapDetails").on("mouseover", "table td", function() {
sideTabHoverEvent(hoverdata[$(this)[0].getElementsByClassName('index')[0].innerText]);
})
function sideTabHoverEvent(e) {
if (e) {
//Set the infobox options with the metadata of the pushpin.
infobox.setOptions({
location: e.getLocation(),
//title: e.metadata.title,
description: e.metadata.description,
visible: true
});
}
}
In you sideTabHoverEnt function you are assuming e is the pushpin the user interacted with, but that's not what you are passing in. You are passing in a string (index as a string). Try doing the following:
function sideTabHoverEvent(e) {
if (e) {
//Turn the index string value into a number.
var idx = parseInt(e);
var shape = map.entities.get(idx);
//Set the infobox options with the metadata of the pushpin.
infobox.setOptions({
location: shape.getLocation(),
//title: shape.metadata.title,
description: shape.metadata.description,
visible: true
});
}
}

Creating a geojson-vt and displaying in Leaflet 0.7

I have a GEOJSON that I am loading up and displaying on a map using Leaflet it is, colour coding them depending on the abundance of the animal in an area and able to display a popup with more information.
What I have been trying to do is turn this into a vectorTile using geojson-vt and display this. This polygon geojson is one of the smaller and takes a long time to load and some others crash the browser.
I have tired to work through the example here http://bl.ocks.org/Sumbera/c67e5551b21c68dc8299 but I cannot understand what is happening.
I am definitely open to other ideas on how to display this data, I am very much a newbie.
If you wish to see what I have so far please look at http://huntingspots.co.nz/gareth/leaflet_layering.html.
Thank you very much for your time.
//Pig Layer
var pigLayer = new L.LayerGroup();
$.getJSON("feralpig.geojson",function(hoodData){
L.geoJson( hoodData, {
style: function(feature){
var fillColour,
abundance = feature.properties.Abundance;
if (abundance == "H") fillColour = "#194866";
else if ( abundance == "M" ) fillColour = "#2c7eb2";
else if ( abundance == "L" ) fillColour = "#40b5ff";
else fillColour = "#f7f7f7"; // no data
return { color: "#999", weight: 1, fillColor: fillColour, fillOpacity: .4 };
},
onEachFeature: function( feature, layer ){
layer.bindPopup( "<strong>" + "Species: " + "</strong>" + feature.properties.Scientific
+ "<br/>" + "<strong>" + "Common Name: " + "</strong>" + feature.properties.CommonName
+ "<br/>" + "<strong>" + "Abundance: " + "</strong>" + feature.properties.Abundance
+ "<br/>" + "<strong>" + "Data collection method: " + "</strong>" + feature.properties.AbundRelia
+ "<br/>" + "<strong>" + "Notes: " + "</strong>" + feature.properties.AbundNotes);
}
}).addTo(pigLayer);
});