biopama_protected_areas-8.x-1.3/js/search/search_map_interaction.js
js/search/search_map_interaction.js
jQuery(document).ready(function($) {
thisMap.on('click', getFeatureInfo);
function getFeatureInfo(e){
var CountrySelected = countryCheck(e);
var pasSelected = paCheck(e);
var countryText = '';
var pasText = '';
if (typeof CountrySelected !== 'undefined' ){
countryText = "<div class='node-pop-country'>" + CountrySelected + "</div>";
}
if (typeof pasSelected !== 'undefined' ){
pasText = "<div class='node-pop-pa'>Protected Areas:<br>" + pasSelected + "</div>";
}
var paPopupContent = "<div class='node-pop-wrapper'>" +
countryText +
pasText +
"</div>";
if (paPopupContent.length > 63){
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML(paPopupContent)
.addTo(thisMap);
}
}
function countryCheck(e){
var country = getMapCountry(e.point);
if (typeof country !== 'undefined') {
var countrySelected = 'disabled';
if (selSettings.iso2 != country.properties.iso2){
countrySelected = 'enabled';
}
var countryLink = "Country: <a class=' "+countrySelected+"' href='/country/"+country.properties.iso2+"'>" + country.properties.original_n + "</a>";
return countryLink;
}
}
function paCheck(e){
var PAs = getMapPAs(e.point);
if (typeof PAs !== 'undefined') {
var paLinks = "";
for (var key in PAs) {
paLinks = paLinks + "<a href='/pa/"+PAs[key].properties.WDPAID+"'>" + PAs[key].properties.NAME + "</a><br>";
}
return paLinks;
}
}
});
var homepageMapZoomOptions = {
padding: {top: 20, bottom:20, left: 20, right: 20}
};
/* (function ($, Drupal) {
$( ".view-id-menu_level_1_policies_:visible" ).trigger('RefreshView')
})(jQuery, Drupal); */
function zoomToCountry(iso2){
var result;
if(iso2 === 'FJ'){
thisMap.fitBounds([[166.61,-26.39], [192.01,-9.62]], homepageMapZoomOptions);
} else if (iso2 === 'TV'){
thisMap.fitBounds([[168.58,-13.60], [191.48,-3.88]], homepageMapZoomOptions);
} else if (iso2 === 'KI'){
thisMap.fitBounds([[-201.57,-15.70], [-136.18,10.31]], homepageMapZoomOptions);
} else {
jQuery.ajax({
url: DOPAgetCountryExtent+iso2,
dataType: 'json',
success: function(d) {
thisMap.fitBounds(jQuery.parseJSON(d.records[0].extent), homepageMapZoomOptions);
},
error: function() {
console.log("Something is wrong with the REST servce for country bounds")
}
});
}
}
function zoomToPA(wdpaid){
selSettings.WDPAID = wdpaid;
jQuery.ajax({
url: DOPAgetWdpaExtent+wdpaid,
dataType: 'json',
success: function(d) {
thisMap.fitBounds(jQuery.parseJSON(d.records[0].extent), homepageMapZoomOptions);
selSettings.paName = d.records[0].name;
if (d.records[0].iso2 != selSettings.ISO2){
selSettings.ISO2 = d.records[0].iso2;
}
},
error: function() {
console.log("Something is wrong with the REST servce for PA bounds")
}
});
}
//returns the first country object
function getMapCountry(point){
var feature = thisMap.queryRenderedFeatures(point, {
layers:["countryMask"],
});
//as long as we have something in the feature query
if (typeof feature[0] !== 'undefined'){
return feature[0];
}
}
//returns a list of PA objects
function getMapPAs(point){
var paFeatures = thisMap.queryRenderedFeatures(point, {
layers:["wdpaAcpMask"]
});
if (typeof paFeatures[0] !== 'undefined'){
return paFeatures;
}
}