This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging
What is the Source
- Data source and format. The simple idea is to use
layers
(layer), different layers need to pass in different data types before the map can be rendered. The data format they need is passedSource
By definition, we just need to pass the existing data into the data source according to the rules, and we don’t need to care about other operations.
Some data types for the Source
ol.source.BingMaps
Bing Map block data layer source.ol.source.CartoDB
CartoDB Maps API layer source.ol.source.Cluster
Cluster vector data.ol.source.Vector
Provides vector layer data.ol.source.Image
Provides a single image data type.ol.source.ImageCanvas
The data source is a Canvas element, where the data is an image.ol.source.ImageMapGuide
Mapguide server provides picture map data.ol.source.ImageStatic
Provides a single static picture map.ol.source.ImageVector
The data source is a Canvas element, but the data in it is a vector source.ol.source.ImageWMS
Single image data provided by the WMS service.ol.source.MapQuest
Slice data provided by MapQuest.ol.source.Stamen
Map slice data provided by Stamen.ol.source.Tile
Provides image data that is segmented into grid slices.ol.source.TileVector
Vector data divided into grids.ol.source.TileDebug
Data is not fetched from the server.ol.source.TileImage
Provide picture data for cutting into slices.ol.source.TileUTFGrid
UTFGrid interactive data in TileJSON format.ol.source.TileJSON
Slice data in TileJSON format.ol.source.TileArcGISRest
Slice data provided by ArcGIS Rest service.ol.source.WMTS
Slice data provided by the WMTS service.ol.source.Zoomify
Slice data in Zoomify format.ol.source.OSM
Slice data provided by OpenStreetMap.ol.source.XYZ
Layer source with slice data for a set of URLS in XYZ format defined in the URL template.
Use Source through Layer
Ol.source. XYZ Slice data source
- in
ol.layer.Tile
Layer.
var layerTile = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'})})Copy the code
- Obtain amap slice data through URL.
Ol.source. Vector Indicates the data source of the Vector layer
- Commonly used attributes
attribution
The prompt message in the lower right corner of the map, passing in the string.features
Geographical elements. The incomingol.Feature
Object.url
The address of the element data.format
url
Properties are seturl
The format of the returned element. The incomingol.format
Format.
- use
features
Load data.
var polygon = new ol.geom.Polygon([
[
[37.19],
[43.19],
[43.3],
[37.3],
[37.19]
]
])
polygon.applyTransform(ol.proj.getTransform('EPSG:4326'.'EPSG:3857'))
// Polygon elements
var polygonFeature = new ol.Feature(polygon)
// Vector layers
var source = new ol.source.Vector({ features: [polygonFeature] })
var vectorLayer = new ol.layer.Vector({
source: source,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
/ / line style
color: '#ffcc33'.width: 2
})
})
})
map.addLayer(vectorLayer)
Copy the code
- In addition to using functions to create element data, you can also use GeoJSON format data.
// The data is in the GeoJSON format
const geojsonObject = {
type: 'FeatureCollection'.crs: {
type: 'name'.properties: {
name: 'EPSG:3857'}},features: [{type: 'Feature'.geometry: {
type: 'Polygon'.coordinates[[[-5e6.6e6], [...5e6.8e6], [...3e6.8e6], [...3e6.6e6], [...5e6.6e6[]]}}, {type: 'Feature'.geometry: {
type: 'Polygon'.coordinates[[[-2e6.6e6], [...2e6.8e6],
[0.8e6],
[0.6e6], [...2e6.6e6[]]}}, {type: 'Feature'.geometry: {
type: 'Polygon'.coordinates[[[:1e6.6e6],
[1e6.8e6],
[3e6.8e6],
[3e6.6e6],
[1e6.6e6[]]}}, {type: 'Feature'.geometry: {
type: 'Polygon'.coordinates[[[-2e6, -1e6], [...1e6.1e6],
[0, -1e6], [...2e6, -1e6[]]}}]}// Vector layers
var source = new ol.source.Vector({ features: new ol.format.GeoJSON().readFeatures(geojsonObject) })
var vectorLayer = new ol.layer.Vector({
source: source,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
/ / line style
color: '#ffcc33'.width: 2
})
})
})
map.addLayer(vectorLayer)
Copy the code
GeoJSON
Is a format for encoding various geographic data structures. It is also our most common data format.- General use
url
This is also the format that you get.
- because
Source
There are many data types, each with its own parameters, events, and so on. I’m not going to go through all of this, but we’ll talk about how to use different layers later. - Need to remember
source
是layer
Defines the source of the map data. andsource
Supports multiple number formats.