1. 程式人生 > >openlayers-熱地圖加載(完整版及代碼)

openlayers-熱地圖加載(完整版及代碼)

初始 sele coo source stat ima layout tap 中心

//地圖加載
function mapInit(data){
//底圖
// var raster = new ol.layer.Tile({
// source: new ol.source.Stamen({
// layer: ‘toner‘
// })
// });
var projection = new ol.proj.Projection({
code: ‘EPSG:4326‘,// ||mapdata.code,
extent: [data.map.minX, data.map.minY, data.map.maxX, data.map.maxY],
});
var view = new ol.View({
center: [data.map.cx, data.map.cy],
projection: projection,
zoom: data.map.zoom,
maxZoom:data.map.maxZoom,
minZoom:data.map.minZoom,
zoomFactor:1.2
});
var opts = {
doubleClickZoom:false,
DragRotateAndZoom:false
};
var map = new ol.Map({
target: ‘map‘,
view:view,
interactions: ol.interaction.defaults(opts)
});
//熱力圖數據 GeoJSON默認參考坐標系為 EPSG:4326.,根據實際需要進行更改
var heatData = [
{
type: "FeatureCollection",
features: [
{ type: "Point", "coordinates": [117.363228, 32.939667], count: 80 },
{ type: "Point", "coordinates": [117.359623, 32.941612], count: 60 },
{ type: "Point", "coordinates": [117.365631, 32.94651], count: 90 },
{ type: "Point", "coordinates": [117.36443, 32.928789], count: 80 },
{ type: "Point", "coordinates": [117.351212, 32.95328], count: 60 },
{ type: "Point", "coordinates": [117.363228, 32.936281], count: 90 },
{ type: "Point", "coordinates": [117.385029, 32.948383], count: 60 },
{ type: "Point", "coordinates": [117.370781, 32.920144], count: 80 },
{ type: "Point", "coordinates": [117.393097, 32.936137], count: 80 },
{ type: "Point", "coordinates": [117.373528, 32.919856], count: 80 },
{ type: "Point", "coordinates": [117.353443, 32.916541], count: 80 },
{ type: "Point", "coordinates": [117.397217, 32.913803], count: 80 },
{ type: "Point", "coordinates": [117.35207, 32.904148], count: 80 }
]
}];
//矢量圖層 獲取geojson數據
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(heatData[0], {
dataProjection: ‘EPSG:4326‘,
featureProjection: ‘EPSG:4326‘
})
});
// Heatmap熱力圖
var vector = new ol.layer.Heatmap({
source: vectorSource,
opacity: [0, 0.8],//透明度
shadow: 250,//陰影像素大小,默認250
//矢量圖層的渲染模式:
//‘image‘:矢量圖層呈現為圖像。性能出色,但點符號和文本始終隨視圖一起旋轉,像素在縮放動畫期間縮放。
//‘vector‘:矢量圖層呈現為矢量。即使在動畫期間也能獲得最準確的渲染,但性能會降低。
renderMode: ‘vector‘
});
windowMapChe = map;
map.addLayer(vector);

if(data.map.mapType == 3){//底圖格式為DXF
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
projection: view.getProjection(),
url: getBaseURL()+"module/uploads/"+data.map.mapUrl,
format: new ol.format.GeoJSON()
})
});
}else{
var layer = new ol.layer.Image({
source: new ol.source.ImageStatic({
url: getBaseURL()+"module/uploads/"+data.map.mapUrl,
projection: view.getProjection(),
imageExtent: [data.map.minX,data.map.minY,data.map.maxX,data.map.maxY]
})
});
}
layer.setZIndex(0);
map.addLayer(layer);
$(‘.ol-attribution‘).hide()

//聚焦地圖指定位置
loc =[data.map.cx,data.map.cy];
// 聚焦地圖指定位置 按指定縮放比例
focusTo(loc,-1);
// 添加地圖左側工具
initTools()
}
/**
* 聚焦地圖指定位置 按指定縮放比例
*/
function focusTo(location,zoomTo){
var zoom = zoomTo||(windowMapChe.get("suitzoom")*1);
var duration = 500;
var pan = ol.animation.pan({
duration: duration,
source: /** @type {ol.Coordinate} */ (windowMapChe.getView().getCenter()),
});
var zooma = ol.animation.zoom({
duration: duration,
resolution: 1 * windowMapChe.getView().getResolution()
});
if(-1==zoom){
zoom =windowMapChe.get("suitzoom")*1;
windowMapChe.beforeRender(pan);
}else{
windowMapChe.beforeRender(zooma,pan);
}
windowMapChe.getView().setCenter(location);
windowMapChe.getView().setZoom(zoom);
//初始化設置中心位置
var params = {
zoom:zoom,
center:windowMapChe.getView().setCenter(location)
};
windowMapChe.setProperties({"suitzoom":params.zoom||1});
windowMapChe.setProperties({"recenter":params.center||location});
}
function initTools(){
// 添加自定義業務操作工具欄
var btns =[
// 工具條顯示隱藏控制鈕
{id:"toolbar-tool",tagName:‘li‘,className:‘arrow_down ctl‘, title:"工具欄",trigger:‘click‘,callback:toolToggle },
// 地圖放大按鈕
{id:"toolbar-zoomin",className:‘zoomin tool‘,title:‘放大‘,trigger:‘click‘,callback:toolMapZoomIn},
// 地圖縮小按鈕
{id:"toolbar-zoomout",className:‘zoomout tool‘,title:‘縮小‘,trigger:‘click‘,callback:toolMapZoomOut},
// 地圖還原位置和縮放比例
{id:"toobar-refresh",className:‘refresh tool‘,title:‘還原‘,trigger:‘click‘,callback:toolRefresh}
];
for(var i in btns){
addBusinessTool({btns:btns[i]});// btns:btns
}

$(".maptool>button").each(function(){
if(!$(this).hasClass("zoomin")
&&!$(this).hasClass("zoomout")
&&!$(this).hasClass("layout")
&&!$(this).hasClass("refresh")){
$(this).on(‘click‘,function(){
if($(this).hasClass("active")){
$(this).removeClass("active");
}else{
$(this).addClass("active");
}
});
}
});
$(".maptool.tool").each(function(e){
$(this).hide();
});
$(".ol-zoom, .ol-zoomslider").remove();
// 添加地圖元素事件
// addMapElementEvent(); 鼠標移動及點擊事件

}
//工具條顯示隱藏按鈕
function toolToggle(){
if($(this).hasClass("arrow_up")){
$(this).removeClass("arrow_up");
$(this).addClass("arrow_down");
$(".ol-control, .maptool, .tool").each(function(e){
if(!$(this).hasClass("arrow_down") && !$(this).hasClass("person") && !$(this).hasClass("area") && !$(this).hasClass("device") && !$(this).hasClass("building")){
$(this).addClass("hide");
}
});
}else{
$(this).removeClass("arrow_down");
$(this).addClass("arrow_up");
$(".ol-control, .maptool, .tool").each(function(e){
$(this).show().removeClass("hide");
});
$(‘.ol-logo-only‘).hide()//地圖右側小圖標
}
}
//地圖放大按鈕事件
function toolMapZoomIn(){
var view = windowMapChe.getView();
var zoom = view.getZoom();
view.setZoom(zoom + 1);
}
// 地圖縮小按鈕事件
function toolMapZoomOut(){
var view = windowMapChe.getView();
var zoom = view.getZoom();
view.setZoom(zoom - 1);
}
//還原地圖位置和縮放比例
function toolRefresh(){
var center = windowMapChe.get("recenter");
var zoom = windowMapChe.get("suitzoom");
var duration = 1500;
var start = +new Date();
var pan = ol.animation.pan({
duration: duration,
source: /** @type {ol.Coordinate} */ (windowMapChe.getView().getCenter()),
start: start
});
var bounce = ol.animation.bounce({
duration: duration,
resolution: 1.1 * windowMapChe.getView().getResolution(),
start: start
});
var zooma = ol.animation.zoom({
duration: duration,
resolution: 1 * windowMapChe.getView().getResolution(),
start: start
});
windowMapChe.beforeRender(bounce,pan,zooma);
windowMapChe.getView().setCenter(center);
windowMapChe.getView().setZoom(zoom);
}
/**
* 添加地圖業務工具欄
*/
function addBusinessTool(opt){
businessTool = function(opt_options) {
var options = opt_options || {};
var element = document.createElement(‘div‘);
element.className=‘maptool ol-unselectable ol-control ‘+opt_options.btns.className;
var button =createToolbarBtn(options.btns);
element.appendChild(button);
ol.control.Control.call(this, {
element: element,
target: options.target
});

};
ol.inherits(businessTool, ol.control.Control);
windowMapChe.addControl(new businessTool(opt));
}
function createToolbarBtn(data){
var ele = document.createElement("button");
ele.className=data.className;
ele.id=data.id;
ele.title=data.title;
$(ele).on(data.trigger,data.callback);
return ele;
}

openlayers-熱地圖加載(完整版及代碼)