基础模板
基础
- import React, { createRef, useEffect, useRef } from 'react';
- import { Map, View, Feature, Overlay } from 'ol';
- import reactDom from 'react-dom';
- import TileLayer from 'ol/layer/Tile';
- import { OSM } from 'ol/source';
- import { Point, LineString } from 'ol/geom'; //点
- import { Icon, Stroke, Style } from 'ol/style';
- import VectorLayer from 'ol/layer/Vector';
- import VectorSource from 'ol/source/Vector';
- import 'ol/ol.css';
- import styles from './index.less';
- import poiImg from '@/assets/images/house.png';
- import ycdl from '../json/ycdl.json'
- const Index: React.FC = () => {
- // 地图
- const map = useRef
- // 地图容器
- const mapDom = createRef
- useEffect(() => {
- // 初始化地图
- initMap()
- // 加载标注
- createFeature(ycdl.features[0].geometry.coordinates, poiImg, 'iconFeature');
- //加载线
- createLine(ycdl.features[0].geometry.coordinates, poiImg, 'lineFeature');
- //加载信息框
- createInfo()
- * 初始化地图
- const initMap = () => {
- map.current = new Map({
- target: 'map',
- layers: [
- new TileLayer({
- source: new OSM(),
- }),
- ],
- view: new View({
- center: [111.286962, 30.69217],
- projection: 'EPSG:4326',
- zoom: 14,
- }),
- //创建标注
- const createFeature = (featureData: Array) => {
- , featureImg: string, layerId: string
- // 创建标注
- if (featureData.length > 0) {
- let iconFeatures: any;
- iconFeatures = new Array();
- featureData.forEach((v, index) => {
- iconFeatures.push(new Feature({
- geometry: new Point([...v])
- iconFeatures[index].setId(`icon ${index}`)
- // 将标注属性添加到矢量数据源中
- let iconSource = new VectorSource({
- features: iconFeatures
- //将标注矢量数据源添加到矢量层
- let iconLayer = new VectorLayer({
- source: iconSource,
- zIndex: 3,
- style: () => {
- return new Style({
- image: new Icon({
- anchor: [0.5, 1],
- scale: 0.12,
- src: featureImg
- // 将矢量层添加到地图
- map.current.addLayer(iconLayer);
- * 创建线
- const createLine = (lineData: Array) => {
- , lineColor: string, layerId: string
- let lineFeature: any;
- if (lineData.length > 0) {
- lineFeature = new Feature({
- geometry: new LineString([...lineData])
- // 将划线属性添加到矢量数据源中
- let lineSource = new VectorSource({})
- lineSource.addFeature(lineFeature);
- let lineLayer = new VectorLayer({
- source: lineSource,
- zIndex: 2,
- style: () => {
- return new Style({
- stroke: new Stroke({
- width: 4,
- color: 'rgb(0,255,0)'
- // 将矢量层添加到地图
- map.current.addLayer(lineLayer);
- * 创建信息框
- const createInfo = () => {
- //创建overlay容器
- const overlayDom = document.createElement('div');
- overlayDom.className = 'overlayDom';
- mapDom.current?.appendChild(overlayDom);
- //创建内部信息容器
- const overlayInfo = document.createElement('div');
- overlayInfo.className = 'overlayInfo';
- overlayDom.appendChild(overlayInfo)
- //创建overlay
- const overlay = new Overlay({
- element: overlayDom, // overlay包含的DOM
- autoPan: true, // 当overlay超出地图边界时,地图自动移动
- positioning: 'bottom-center',
- offset: [0, -50], // 偏移量,单位像素
- stopEvent: true, // 事件传播
- autoPanAnimation: {
- // 设置autoPan的动画
- duration: 500
- },
- autoPanMargin: 150
- //将overlay添加到地图
- map.current.addOverlay(overlay)
- //设置地图左键点击事件
- map.current.on('click', (event: any) => {
- if (overlayInfo.innerHTML !== ' ') {
- // 清空
- reactDom.unmountComponentAtNode(overlayInfo);
- const feature = map.current.forEachFeatureAtPixel(event.pixel, function (feature: any, vectorLayer: any) {
- return feature;
- if (feature && feature.id_ != undefined) {
- const infoData = feature.getGeometry().flatCoordinates;
- const infoDom = (
- 经度: {infoData[0]}, 维度:{infoData[1]}
- div>
- //将overlayInfo插入infoDom
- reactDom.render(infoDom, overlayInfo);
- // 设置overlay位置
- overlay.setPosition(infoData)
- return (
- span>
- export default Index
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.