效果

代码
var key = 自己去天地图官网申请;
var layers = [];
var TiandiMap_vec = new ol.layer.Tile({
name: "天地图矢量图层",
source: new ol.source.XYZ({
url: "http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=" + key,
crossOrigin: 'anonymous',
wrapX: false
})
});
var Tianditu_cva = new ol.layer.Tile({
name: "天地图矢量注记图层",
source: new ol.source.XYZ({
url: "http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=" + key,
crossOrigin: 'anonymous',
wrapX: false
})
});
var TiandiMap_img = new ol.layer.Tile({
name: "天地图影像图层",
source: new ol.source.XYZ({
url: "http://t0.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=" + key,
crossOrigin: 'anonymous',
wrapX: false
})
});
var TiandiMap_cia = new ol.layer.Tile({
name: "天地图影像注记图层",
source: new ol.source.XYZ({
url: "http://t0.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=" + key,
crossOrigin: 'anonymous',
wrapX: false
})
});
var TiandiMap_cia88 = new ol.layer.Tile({
name: "天地图边界线图层",
source: new ol.source.XYZ({
url: "http://t0.tianditu.com/DataServer?T=ibo_w&x={x}&y={y}&l={z}&tk=" + key,
crossOrigin: 'anonymous',
wrapX: false
})
});
var source = new ol.source.Vector({
wrapX: false
});
var vector = new ol.layer.Vector({
name:'vector',
source: source,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.2)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
});
layers.push(TiandiMap_vec)
layers.push(TiandiMap_img)
layers.push(TiandiMap_cia88)
layers.push(Tianditu_cva)
layers.push(TiandiMap_cia)
layers.push(vector)
var mousePositionControl = new ol.control.MousePosition({
coordinateFormat: ol.coordinate.createStringXY(4),
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: document.getElementById('mouse-position'),
undefinedHTML: ' '
});
var scaleLineControl = new ol.control.ScaleLine({
units: "metric"
});
let map = new ol.Map({
layers: layers,
target: 'mapCon',
view: new ol.View({
projection: "EPSG:3857",
center: ol.proj.transform([109.49081,36.596537], 'EPSG:4326', 'EPSG:3857'),
zoom: 8
,maxZoom: 17
,minZoom: 3,
}),
controls: ol.control.defaults({
attributionOptions: ({
collapsible: true
})
}).extend([
new ol.control.ZoomToExtent({
extent: [
813079.7791264898, 5929220.284081122,
848966.9639063801, 5936863.986909639
]
})
, mousePositionControl, scaleLineControl]),
});