ol官方文档
ol官方文档
安装
npm install ol --save
安装后
实例组件
< template>
< div style = " width : 100%; height : 100%" > </ div>
</ template>
< script>
import "ol/ol.css" ;
import Fill from "ol/style/Fill" ;
import Image from "ol/layer/Image" ;
import ImageWMS from "ol/source/ImageWMS" ;
import Text from "ol/style/Text" ;
import TileLayer from "ol/layer/Tile" ;
import VectorLayer from "ol/layer/Vector" ;
import VectorSource from "ol/source/Vector" ;
import XYZ from "ol/source/XYZ" ;
import {
Map,
View,
Feature
} from "ol" ;
import {
Point
} from "ol/geom" ;
import {
Style,
Icon
} from "ol/style" ;
import {
defaults
} from "ol/control/util.js" ;
import markerIcon from "@/assets/images/ship.png" ;
import {
getAnalog
} from "@/utils/uice" ;
let vectorSource = new VectorSource ( {
features : [ ] ,
} ) ;
let vectorLayer = new VectorLayer ( {
source : vectorSource,
} ) ;
let that;
let realTimer;
export default {
name : "tianditu" ,
props : {
data : {
type : Object,
default : ( ) => {
return {
center : [ 116.397428 , 39.90923 ] ,
zoom : 12 ,
} ;
} ,
} ,
} ,
mounted ( ) {
this . tiandituUrl = "http://t0.tianditu.gov.cn/" ;
this . hangdaotuUrl = "http://36.156.155.131:8090/" ;
that = this ;
that. initMap ( ) ;
clearInterval ( realTimer) ;
getAnalog ( ) . then ( ( response ) => {
} ) ;
realTimer = setInterval ( ( ) => {
getAnalog ( ) . then ( ( response ) => {
} ) ;
} , this . $store. state. data. duration) ;
} ,
destroyed ( ) {
clearInterval ( realTimer) ;
realTimer = null ;
} ,
data ( ) {
return {
map : null ,
} ;
} ,
methods : {
initMap ( ) {
this . map = new Map ( {
target : this . $el,
controls : defaults ( {
zoom : false ,
} ) ,
layers : [
new TileLayer ( {
source : new XYZ ( {
url : ` ${ this . tiandituUrl} DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=317e52a409b6b382957e09003ee7e235 ` ,
} ) ,
} ) ,
new TileLayer ( {
source : new XYZ ( {
url : ` ${ this . tiandituUrl} DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=317e52a409b6b382957e09003ee7e235 ` ,
} ) ,
} ) ,
new Image ( {
source : new ImageWMS ( {
url : ` ${ this . hangdaotuUrl} geoserver/CJ/wms ` ,
params : {
SERVICE : "WMS" ,
VERSION : "1.1.1" ,
REQUEST : "GetMap" ,
FORMAT : "image/png" ,
TRANSPARENT : true ,
tiled : true ,
STYLES : "" ,
LAYERS : "CJ:G_CJ_NJ" ,
CJAUTH : "CJUSER" ,
WIDTH : 256 ,
HEIGHT : 256 ,
SRS : "EPSG:3857" ,
} ,
} ) ,
} ) ,
vectorLayer,
] ,
view : new View ( {
projection : "EPSG:4326" ,
center : [ 116.397428 , 39.90923 ] ,
zoom : 13 ,
maxZoom : 19 ,
minZoom : 5 ,
} ) ,
} ) ;
} ,
addFeature ( ship ) {
var newFeature = new Feature ( {
geometry : new Point ( [ Number ( ship[ "0a4" ] ) , Number ( ship[ "0a5" ] ) ] ) ,
name : ship[ "iboatName" ] ,
} ) ;
newFeature. setId ( ship[ "sn" ] ) ;
newFeature. setStyle ( this . getStyls ( newFeature, ship) ) ;
vectorSource. addFeature ( newFeature) ;
} ,
setFeature ( ship ) {
vectorSource. idIndex_[ ship[ "sn" ] ] . setStyle (
this . getStyls ( vectorSource. idIndex_[ ship[ "sn" ] ] , ship)
) ;
vectorSource. idIndex_[ ship[ "sn" ] ] . setGeometryName ( ship[ "iboatName" ] ) ;
vectorSource. idIndex_[ ship[ "sn" ] ] . setGeometry (
new Point ( [ Number ( ship[ "0a4" ] ) , Number ( ship[ "0a5" ] ) ] )
) ;
} ,
updateFeatures ( ) {
that. $store. state. data. analogList;
for ( let i = 0 ; i < that. $store. state. data. analogList. length; i++ ) {
let item = that. $store. state. data. analogList[ i] ;
if ( vectorSource. idIndex_[ item[ "sn" ] ] != undefined ) {
that. setFeature ( item) ;
} else {
that. addFeature ( item) ;
}
}
} ,
getStyls ( feature, ship ) {
let rotation = Math. tan ( Number ( ship[ "0aA" ] ) ) ;
rotation = isNaN ( rotation) ? 0 : rotation;
let Styles = [ ] ;
Styles. push (
new Style ( {
image : new Icon ( {
src : markerIcon,
anchor : [ 0.5 , 0.7 ] ,
rotation : rotation,
} ) ,
} )
) ;
Styles. push (
new Style ( {
text : new Text ( {
textAlign : "center" ,
text : ` ${ feature. values_. name} ` ,
offsetX : 0 ,
offsetY : 15 ,
fill : new Fill ( {
color : "#2520ff" ,
} ) ,
backgroundFill : new Fill ( {
color : "rgba(255,255,255,0)" ,
} ) ,
} ) ,
} )
) ;
return Styles;
} ,
} ,
} ;
</ script>
< style scoped >
</ style>