//vado a registrare la nuova proiezione
ol.proj.proj4.register(proj4);
const firenze_gbw = [1681450.785672498, 4848967.319261061];
const firenze_gmerc = ol.proj.transform(firenze_gbw, "EPSG:3003", "EPSG:3857");
const map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: firenze_gmerc,
zoom: 15
})
});
// uso jquery per poter selezionare i due elementi
const gbw_coords_el = document.querySelector("#gbw_coords");
const wgs84_coords_el = document.querySelector("#wgs84_coords");
const map_dom = map.getTargetElement(); ////
//utilizzando jquery
/*map_dom.on('mouseleave', function() {
gbw_coords_el.text('');
wgs84_coords_el.text('')
})*/
// registriamo l'evento mouseleave sul DOM dell mappa
// che ci servirà per cancellare le coordinate (testo) dai due elementi DOM
// adibiti all visualizzazione delle coordinate
map_dom.addEventListener('mouseleave', function() {
gbw_coords_el.innerText = '';
wgs84_coords_el.innerText = '';
})
//utilizziamo l'evento sulla mappa pointermove
///https://openlayers.org/en/latest/apidoc/module-ol_Map-Map.html
map.on('pointermove', function(event) {
//event è ol.MapBrowserEvent
const gbw_coords = ol.proj.transform(event.coordinate, "EPSG:3857", "EPSG:3003");
const wgs84_coords = ol.proj.transform(event.coordinate, "EPSG:3857", "EPSG:4326");
gbw_coords_el.innerText = formatCoords(gbw_coords);
wgs84_coords_el.innerText = formatCoords(wgs84_coords, 4);
});
// funzione che ha il solo compito di trasformare le coordinate avent dec cifre decimali
function formatCoords(coords, dec) {
dec = dec || 2;
// .toFixed Converte un numero in stringa, lasciando solo n decimali:
return coords[0].toFixed(dec) + ", " + coords[1].toFixed(dec);
};
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.