feat(layer) add fitbounds

This commit is contained in:
thinkinggis 2019-03-05 21:06:12 +08:00
parent b5389f107d
commit 879535f3f8
10 changed files with 119 additions and 134 deletions

View File

@ -30,7 +30,7 @@ const scene = new L7.Scene({
}); });
scene.on('loaded', () => { scene.on('loaded', () => {
$.get('https://gw.alipayobjects.com/os/rmsportal/oVTMqfzuuRFKiDwhPSFL.json', data => { $.get('https://gw.alipayobjects.com/os/rmsportal/oVTMqfzuuRFKiDwhPSFL.json', data => {
scene.PointLayer({ window.layer = scene.PointLayer({
zIndex: 2 zIndex: 2
}) })
.source(data.list, { .source(data.list, {

View File

@ -213,6 +213,10 @@ export default class Layer extends Base {
} }
texture() { texture() {
}
fitBounds() {
const extent = this.layerSource.data.extent;
this.scene.fitBounds(extent);
} }
hide() { hide() {
this._visible(false); this._visible(false);
@ -472,10 +476,10 @@ export default class Layer extends Base {
} }
_initEvents() { _initEvents() {
this.scene.on('pick-' + this.layerId, e => { this.scene.on('pick-' + this.layerId, e => {
const { featureId, point2d, type } = e; let { featureId, point2d, type } = e;
if (featureId < -100 && this._activeIds !== null) { if (featureId < -100 && this._activeIds !== null) {
this.emit('mouseleave'); type = 'mouseleave';
return; featureId = this._activeIds[0];
} }
const feature = this.layerSource.getSelectFeature(featureId); const feature = this.layerSource.getSelectFeature(featureId);
const lnglat = this.scene.containerToLngLat(point2d); const lnglat = this.scene.containerToLngLat(point2d);
@ -483,6 +487,7 @@ export default class Layer extends Base {
featureId, featureId,
feature, feature,
pixel: point2d, pixel: point2d,
type,
lnglat: { lng: lnglat.lng, lat: lnglat.lat } lnglat: { lng: lnglat.lng, lat: lnglat.lat }
}; };
if (featureId >= 0) { if (featureId >= 0) {

View File

@ -2,6 +2,7 @@
import Base from './base'; import Base from './base';
import Controller from './controller/index'; import Controller from './controller/index';
import { getTransform, getParser } from '../source'; import { getTransform, getParser } from '../source';
import { extent } from '../util/geo';
import { getMap } from '../map/index'; import { getMap } from '../map/index';
export default class Source extends Base { export default class Source extends Base {
getDefaultCfg() { getDefaultCfg() {
@ -32,6 +33,7 @@ export default class Source extends Base {
const { type = 'geojson' } = parser; const { type = 'geojson' } = parser;
const data = this.get('data'); const data = this.get('data');
this.data = getParser(type)(data, parser); this.data = getParser(type)(data, parser);
this.data.extent = extent(this.data.dataArray);
} }
/** /**
* 数据统计 * 数据统计
@ -40,7 +42,8 @@ export default class Source extends Base {
const trans = this._transforms; const trans = this._transforms;
trans.forEach(tran => { trans.forEach(tran => {
const { type } = tran; const { type } = tran;
this.data = getTransform(type)(this.data, tran); const data = getTransform(type)(this.data, tran);
Object.assign(this.data, data);
}); });
this._transforms = trans; this._transforms = trans;
} }

View File

@ -1,4 +1,5 @@
precision highp float; precision highp float;
attribute float pickingId;
attribute vec4 a_color; attribute vec4 a_color;
attribute float a_size; attribute float a_size;
attribute float a_shape; attribute float a_shape;

View File

@ -0,0 +1,7 @@
vec4 id_topickColor(float pickingId) {
float id = step(0.,pickingId) * pickingId;
vec3 a = fract(vec3(1.0/255.0, 1.0/(255.0*255.0), 1.0/(255.0*255.0*255.0)) * id);
a -= a.xxy * vec3(0.0, 1.0/255.0, 1.0/255.0);
vec4 worldId = vec4(a,1);
return worldId
}

View File

@ -17,7 +17,7 @@ export default class GaodeMap extends Base {
let d = Math.PI / 180; let d = Math.PI / 180;
let x = lnglat[0] * d; let x = lnglat[0] * d;
let y = lat * d; let y = lat * d;
y = Math.log(Math.tan((Math.PI / 4) + (y / 2))); y = Math.log(Math.tan(Math.PI / 4 + y / 2));
const a = 0.5 / Math.PI, const a = 0.5 / Math.PI,
b = 0.5, b = 0.5,
c = -0.5 / Math.PI; c = -0.5 / Math.PI;
@ -34,14 +34,13 @@ export default class GaodeMap extends Base {
setTimeout(() => { setTimeout(() => {
this.emit('mapLoad'); this.emit('mapLoad');
}, 100); }, 100);
} }
initMap() { initMap() {
const mapStyle = this.get('mapStyle'); const mapStyle = this.get('mapStyle');
switch (mapStyle) { switch (mapStyle) {
case 'dark' : case 'dark':
this.set('mapStyle', Theme.DarkTheme.mapStyle); this.set('mapStyle', Theme.DarkTheme.mapStyle);
break; break;
case 'light': case 'light':
@ -52,7 +51,6 @@ export default class GaodeMap extends Base {
} }
this.set('zooms', [ this.get('minZoom'), this.get('maxZoom') ]); this.set('zooms', [ this.get('minZoom'), this.get('maxZoom') ]);
this.map = new AMap.Map(this.container, this._attrs); this.map = new AMap.Map(this.container, this._attrs);
} }
asyncCamera(engine) { asyncCamera(engine) {
this._engine = engine; this._engine = engine;
@ -64,7 +62,7 @@ export default class GaodeMap extends Base {
let { fov, near, far, height, pitch, rotation, aspect } = mapCamera; let { fov, near, far, height, pitch, rotation, aspect } = mapCamera;
pitch *= DEG2RAD; pitch *= DEG2RAD;
rotation *= DEG2RAD; rotation *= DEG2RAD;
camera.fov = 180 * fov / Math.PI; camera.fov = (180 * fov) / Math.PI;
camera.aspect = aspect; camera.aspect = aspect;
camera.near = near; camera.near = near;
camera.far = far; camera.far = far;
@ -97,32 +95,78 @@ export default class GaodeMap extends Base {
const canvasContainer = document.getElementById(this.container); const canvasContainer = document.getElementById(this.container);
this.canvasContainer = canvasContainer; this.canvasContainer = canvasContainer;
this.renderDom = document.createElement('div'); this.renderDom = document.createElement('div');
this.renderDom.style.cssText += 'position: absolute;top: 0; z-index:1;height: 100%;width: 100%;pointer-events: none;'; this.renderDom.style.cssText +=
'position: absolute;top: 0; z-index:1;height: 100%;width: 100%;pointer-events: none;';
this.renderDom.id = 'l7_canvaslayer'; this.renderDom.id = 'l7_canvaslayer';
canvasContainer.appendChild(this.renderDom); canvasContainer.appendChild(this.renderDom);
} }
mixMap(scene) { mixMap(scene) {
const map = this.map; const map = this.map;
scene.getZoom = () => { return map.getZoom(); }; scene.getZoom = () => {
scene.getCenter = () => { return map.getCenter(); }; return map.getZoom();
scene.getSize = () => { return map.getSize(); }; };
scene.getPitch = () => { return map.getPitch(); }; scene.getCenter = () => {
scene.getRotation = () => { return map.getRotation(); }; return map.getCenter();
scene.getStatus = () => { return map.getStatus(); }; };
scene.getScale = () => { return map.getScale(); }; scene.getSize = () => {
scene.getZoom = () => { return map.getZoom(); }; return map.getSize();
scene.setZoom = () => { return map.setZoom(); }; };
scene.setBounds = () => { return map.setBounds(); }; scene.getPitch = () => {
scene.setRotation = () => { return map.setRotation(); }; return map.getPitch();
scene.zoomIn = () => { return map.zoomIn(); }; };
scene.setRotation = () => { return map.setRotation(); }; scene.getRotation = () => {
scene.zoomOut = () => { return map.zoomOut(); }; return map.getRotation();
scene.panTo = () => { return map.panTo(); }; };
scene.panBy = () => { return map.panBy(); }; scene.getStatus = () => {
scene.setPitch = () => { return map.setPitch(); }; return map.getStatus();
scene.pixelToLngLat = () => { return map.pixelToLngLat(); }; };
scene.lngLatToPixel = () => { return map.lngLatToPixel(); }; scene.getScale = () => {
scene.setMapStyle = () => { return map.setMapStyle(); }; return map.getScale();
};
scene.getZoom = () => {
return map.getZoom();
};
scene.setZoom = () => {
return map.setZoom();
};
scene.setBounds = () => {
return map.setBounds();
};
scene.setRotation = () => {
return map.setRotation();
};
scene.zoomIn = () => {
return map.zoomIn();
};
scene.setRotation = () => {
return map.setRotation();
};
scene.zoomOut = () => {
return map.zoomOut();
};
scene.panTo = () => {
return map.panTo();
};
scene.panBy = () => {
return map.panBy();
};
scene.setPitch = () => {
return map.setPitch();
};
scene.pixelToLngLat = () => {
return map.pixelToLngLat();
};
scene.lngLatToPixel = () => {
return map.lngLatToPixel();
};
scene.setMapStyle = () => {
return map.setMapStyle();
};
scene.fitBounds = extent => {
return map.setBounds(
new AMap.Bounds([ extent[0], extent[1] ], [ extent[2], extent[3] ])
);
};
scene.containerToLngLat = pixel => { scene.containerToLngLat = pixel => {
const ll = new AMap.Pixel(pixel.x, pixel.y); const ll = new AMap.Pixel(pixel.x, pixel.y);
return map.containerToLngLat(ll); return map.containerToLngLat(ll);

View File

@ -1,69 +0,0 @@
export default class GaodeMap {
constructor(map) {
this.map = map;
}
getZoom() {
return this.map.getZoom();
}
getCenter() {
return this.map.getCenter();
}
getSize() {
return this.map.getSize();
}
getPitch() {
return this.map.getPitch();
}
getRotation() {
return this.map.getRotation();
}
getStatus() {
return this.map.getStatus();
}
getScale() {
return this.map.getScale();
}
setZoom(zoom) {
return this.map.setZoom(zoom);
}
setCenter(lnglat) {
return this.map.setCenter(lnglat);
}
setBounds(bounds) {
return this.map.setBounds(bounds);
}
setRotation(rotation) {
return this.map.setRotation(rotation);
}
zoomIn() {
return this.map.zoomIn();
}
zoomOut() {
return this.map.zoomOut();
}
panTo(lngLat) {
return this.map.panTo(lngLat);
}
panBy(x, y) {
return this.map.panBy(x, y);
}
setPitch(pitch) {
return this.map.setPitch(pitch);
}
pixelToLngLat(lngLat, level) {
return this.map.pixelToLngLat(lngLat, level);
}
lngLatToPixel(lngLat, level) {
return this.map.lnglatToPixel(lngLat, level);
}
containerToLngLat(pixel) {
const ll = new AMap.Pixel(pixel.x, pixel.y);
return this.map.containerToLngLat(ll);
}
setMapStyle(style) {
return this.map.setMapStyle(style);
}
}

View File

@ -5,9 +5,9 @@ export default function csv(data, cfg) {
const resultdata = []; const resultdata = [];
csvdata.forEach((col, featureIndex) => { csvdata.forEach((col, featureIndex) => {
let coordinates = []; let coordinates = [];
if (x && y) { coordinates = [ col[x], col[y] ]; } // 点数据 if (x && y) { coordinates = [ col[x] * 1, col[y] * 1 ]; } // 点数据
if (x1 && y1) { // 弧线 或者线段 if (x1 && y1) { // 弧线 或者线段
coordinates = [[ col[x], col[y] ], [ col[x1], col[y1] ]]; coordinates = [[ col[x] * 1, col[y] * 1 ], [ col[x1] * 1, col[y1] * 1 ]];
} }
col._id = featureIndex + 1; col._id = featureIndex + 1;
const dataItem = { const dataItem = {

View File

@ -1,8 +1,3 @@
/**
* @fileOverview The util method based on the lodash.
* @author dxq613@gmail.com
* @see https://github.com/lodash/lodash
*/
import Utils from '@antv/util'; import Utils from '@antv/util';
const Util = Utils.mix({}, Utils, { const Util = Utils.mix({}, Utils, {
@ -13,33 +8,7 @@ const Util = Utils.mix({}, Utils, {
isNaN, isNaN,
snapEqual: Utils.isNumberEqual, snapEqual: Utils.isNumberEqual,
remove: Utils.pull, remove: Utils.pull,
inArray: Utils.contains, inArray: Utils.contains
/**
* 将用户输入的 padding 转换成 [top, right, bottom, right] 的模式
* @param {Number|Array} padding 输入的padding
* @return {Array} 四个padding
*/
toAllPadding(padding) {
let top = 0;
let left = 0;
let right = 0;
let bottom = 0;
if (Util.isNumber(padding) || Util.isString(padding)) {
top = left = right = bottom = padding;
} else if (Util.isArray(padding)) {
top = padding[0];
right = !Util.isNil(padding[1]) ? padding[1] : padding[0];
bottom = !Util.isNil(padding[2]) ? padding[2] : padding[0];
left = !Util.isNil(padding[3]) ? padding[3] : right;
} else if (Util.isObject(padding)) {
top = padding.top || 0;
right = padding.right || 0;
bottom = padding.bottom || 0;
left = padding.left || 0;
}
return [ top, right, bottom, left ];
}
}); });
Util.Array = { Util.Array = {

25
src/util/geo.js Normal file
View File

@ -0,0 +1,25 @@
/**
* 计算地理数据范围
* @param {dataArray} data 地理坐标数据
* @return {Array} extent
*/
export function extent(data) {
const extent = [ Infinity, Infinity, -Infinity, -Infinity ];
data.forEach(item => {
const { coordinates } = item;
if (Array.isArray(coordinates[0])) {
coordinates.forEach(coord => {
if (extent[0] > coord[0]) extent[0] = coord[0];
if (extent[1] > coord[1]) extent[1] = coord[1];
if (extent[2] < coord[0]) extent[2] = coord[0];
if (extent[3] < coord[1]) extent[3] = coord[1];
});
} else {
if (extent[0] > coordinates[0]) extent[0] = coordinates[0];
if (extent[1] > coordinates[1]) extent[1] = coordinates[1];
if (extent[2] < coordinates[0]) extent[2] = coordinates[0];
if (extent[3] < coordinates[1]) extent[3] = coordinates[1];
}
});
return extent;
}