Quiero saber la latitud y la longitud de las cuatro esquinas del mapa que se muestra cuando arrastro el mapa. Entonces, creo un código para obtener la longitud y la latitud de las cuatro esquinas, en el momento en que ocurre un evento de arrastre.
Pruebo este código. El código captura el evento y se activa el controlador de eventos. Sin embargo, cuando ejecuté map.getBound (), el intérprete devolvió un error. El mensaje de error es el siguiente
'Error de tipo no detectado: no se pueden leer las propiedades de undefined (leyendo 'getBounds')'
Estoy confundido. A partir del mensaje de error, es posible que la instancia del mapa no se haya creado, pero también es posible que el controlador de eventos esté funcionando y se haya creado.
¿Qué está mal y cómo puedo obtener el tamaño del mapa en el controlador de eventos?
el codigo esta aqui
let Map; // map instance let X; // map position(longitude) let Y; // map position(latitude) let Z; // map zoom ratio let ColorIndex; // item color index let ColorCode; // item color code /** * @brief main program * @details main program * initialize then show contents */ function WebViewMain() { // initialize OSM_CoreInit(); OSM_EventHandle(); } function OSM_CoreInit() { this.X = 0.0; // map position(longitude) this.Y = 0.0; // map position(latitude) this.Z = 1; // map zoom ratio this.Map = new L.Map('map').setView([this.X, this.Y], this.Z); let tiles = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', { maxZoom: 18, attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, ' + 'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>', id: 'mapbox/streets-v11', tileSize: 512, zoomOffset: -1 }); tiles.addTo(this.Map); } function OSM_EventHandle() { this.Map.on( 'moveend', function(e){ alert( this.Map.getBounds()); }); this.Map.on( 'click', function(e){ let aa = e.latlng; }); }gracias.
Cuando llamas a this en una función, usa el contexto de la función y no tu clase:
this.Map.on( 'moveend', function(e){ alert( this.Map.getBounds()); // this is now the event function and not the class, so there is no this.Map object });Necesitas pasar el contexto de la clase. Cambiar a:
this.Map.on( 'moveend', (e)=>{ alert( this.Map.getBounds()); });o
this.Map.on( 'moveend', function(e){ alert( this.Map.getBounds()); }, this);o
this.Map.on( 'moveend', function(e){ alert( this.Map.getBounds()); }.bind(this));