Skip to content

API (2D API)

useMap Hooks提供了基于 OpenLayers 的API封装。

getTargetId

获取地图容器的id,地图容器为<div>元素。(唯一性id)。

示例:

typescript
const { getTargetId } = useMap('id');
console.log('id', getTargetId());

getInstance

获取地图实例,地图实例为ol.Map对象。

示例:

typescript
const { getInstance } = useMap('id');
console.log('地图实例', getInstance());

getTargetElement

获取地图容器的<div>元素。在设置鼠标样式时非常有用。

示例:

typescript
const { getTargetElement } = useMap('id');
console.log('地图容器元素:', getTargetElement());

addMarker

创建点位,点位为ol.Feature对象。根据图层名称、点位数据、点位样式创建点位。其中点位样式为ol.style.Style对象。

参数:

NameTypeDescription
layerNameString图层名称
dataArray数据格式为数组对象
optionsLayerOptions['Point']配置项

options配置项:

NameTypeDescription
styleol.style.Style样式
getStyle(layerName, feature) => ol.style.Style样式 , style优先级最高
zIndexNumber层级
visibleBoolean是否可见
lonLabelString经度字段名称
latLabelString纬度字段名称

示例:

typescript
const { addMarker } = useMap('id');
addMarker('laierName', [{lon:120.0, lat:30.0}], {
    style: new ol.style.Style({
        image: new CircleStyle({
          radius: 5,
          fill: new Fill({ color: 'blue' })
        })
    }),
    // getStyle: (layerName, feature) => {
    //     return new ol.style.Style({
    //         image: new CircleStyle({
    //           radius: 5,
    //           fill: new Fill({ color: 'blue' })
    //         })
    //     }
    // },
    visible: true,
    zIndex: 10,
    lonLabel: 'lon',
    latLabel: 'lat',
})

createLayer

创建矢量图层,图层为ol.layer.Vector对象。根据图层名称、点位数据、点位样式创建点位。其中点位样式为ol.style.Style对象。其中配置项type字段支持Point、GeoJSON、LineString、Polygon、Circle、MultiLineString、MultiPolygon等类型,不同类型data数据结构不一样, addMarker就是这个方法type为Point的一个封装。

参数:

NameTypeDescription
layerNameString图层名称
dataArray数据格式为数组对象
optionsLayerOptions['Point']配置项

options配置项:

NameTypeDescription
typeString支持 Point、GeoJSON、LineString、MultiLineString、Polygon、MultiPolygon、Circle
styleol.style.Style样式
getStyle(layerName, feature) => ol.style.Style样式 , style优先级最高
zIndexNumber层级
visibleBoolean是否可见
lonLabelString经度字段名称
latLabelString纬度字段名称

如果typePoint,则data格式为:

typescript
[{
    lon: 120.0,
    lat: 30.0,
    ...
}]

如果typeGeoJSON,则data格式为:

typescript
{
    type: "FeatureCollection",
    features: [
        {
            type: "Feature",
            geometry: {
                type: "Point",
                coordinates: [102.0, 0.5],
            },
            properties: {}
        }
    ] 
}

如果typeLineString,MultiLineString,Polygon,MultiPolygon,则data格式为:

typescript
[
    {
        coordinates: [[102.0, 0.5], [103.0, 0.5], [104.0, 0.5]]
    }
]

如果typeCircle,则data格式为:

typescript
{
    lon: 120.0,
    lat: 30.0,
    ...
}

options参数为:

typescript
{
   radius: 1000, // 圆形半径
   style: new ol.style.Style({
        image: new ol.style.Circle({
            radius: 7,
            fill: new ol.style.Fill({
                color: 'rgba(255, 255, 255, 0.2)',
            }),
            stroke: new ol.style.Stroke({
                color: '#319FD3',
                width: 2,
            }),
        }),
   })
}

示例:

typescript
const { createLayer } = useMap('id');
createLayer('laierName', [{lon:120.0, lat:30.0}], {
    type: 'Point', // 支持 Point、GeoJSON、LineString、Polygon、Circle、MultiLineString、MultiPolygon
    style: new ol.style.Style({
        image: new CircleStyle({
          radius: 5,
          fill: new Fill({ color: 'blue' })
        })
    }),
    // getStyle: (layerName, feature) => {
    //     return new ol.style.Style({
    //         image: new CircleStyle({
    //           radius: 5,
    //           fill: new Fill({ color: 'blue' })
    //         })
    //     }
    // },
    visible: true,
    zIndex: 10,
    lonLabel: 'lon',
    latLabel: 'lat',
})

createWmsLayer

创建WMS图层

参数:

NameTypeDescription
layerNameString图层名称
optionsWmsOptions配置项

options配置项:

NameTypeDescription
urlStringwms服务地址
zIndexNumber层级
visibleBoolean是否可见
opacityNumber透明度0-1
cqlFilterString是否可见
layersStringwms图层服务名称

示例:

js
const { createWmsLayer } = useMap('id');
map.createWmsLayer('wmsLayer', {
    url: 'http://localhost:8080/geoserver/wms',
    layers: 'test:province',
    zIndex: 10,
    visible: true,
    opacity: 1,
    cqlFilter: 'name = "北京市"',
})

createOverlay

创建覆盖物

参数:

NameTypeDescription
layerNameString覆盖物名称
optionsOverlayOptions配置项

options配置项:

NameTypeDescription
positioningPositioning位置

返回值:

NameTypeDescription
overlayOverlayerOverlayer实例
elementHTMLElement覆盖物绑定上下文

示例:

typescript
   const { overlay, element } = createOverlay('layerName', { positioning: 'center' })
   element.innerHTML = '<div style="color: red;">2323</div>'
   overlay.setPosition([104.064839, 30.548857])

Released under the MIT License.