openlayers adaptive view (13)

In the development of gis applications, I often encounter the need to change the view to display a certain area, such as switching each city. My previous method was to calculate the center point and zoom level setting to the view through the zoning data, such as:

let center = [x, y]
let zoom = 15
map.getView().setCenter(center)
map.getView().setZoom(zoom)

Later I wondered if I could go to adaptive center and zoom. I found the fit method in the view by looking through the official document. Let’s take a look at the introduction of the official document: The
Insert picture description here
fit method can pass 2 parameters.
The first one is a geometric figure or a range. In fact, the two are the same. It will take the maximum and minimum values ​​of the longitude and latitude inside to form a rectangle, which is the range of the map display.
The second is the configuration, the configuration can set some limits and padding.

For example:
I have 2 longitudes and 2 latitudes, which are X1, X2, Y1, and Y2. Through these 4 parameters, I can get a range. Then give this range to the fit method. After the code is executed, the view will adapt to display this range.
If it is a group of many data, just take out the minimum and maximum latitude and longitude and assign them to X1, X2, Y1, and Y2.

map.getView().fit([
	X1,Y1,X2,Y2
])

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_37797410/article/details/108419112