I am trying to display a map of the USA in OpenLayers, and am trying to fit it to a layer. I tried map.getView().fit(Usa.getSource().getExtent());
but that didn't do anything. I thought that maybe the extent was the same as the map for some reason, but no other lines of code would run after that line of code. I am not experienced with openlayers, and javascript is not my first language, so I assume the problem is something silly that I have overlooked. Here is all of the code:
const Usa = new VectorLayer({
source: new VectorSource({
url: '/data/Usa.geojson',
format: new GeoJSON(),
}),
});
const map = new Map ({
view: new View({
center: [0,0],
zoom: 1,
projection: 'EPSG:3857',
}),
target: 'map-container',
});
map.addLayer(Usa);
map.getView().fit(Usa.getSource().getExtent());
Usa.getSource().on('featuresloadend', () => { map.getView().fit(Usa.getSource().getExtent()); });