Merge pull request #275 from antvis/l7-draw

fix: 修复版本锁定&marker事件
This commit is contained in:
@thinkinggis 2020-04-07 11:43:12 +08:00 committed by GitHub
commit a75e88715e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 172 additions and 91 deletions

1
.gitignore vendored
View File

@ -79,3 +79,4 @@ node_modules/
packages/l7/package_bak.json packages/l7/package_bak.json
stories/Test stories/Test
packages/draw/node_modules/@turf

View File

@ -79,7 +79,7 @@ const World = React.memo(function Map() {
).then((d) => d.json()), ).then((d) => d.json()),
// https://lab.isaaclin.cn/nCoV/api/area?latest=1 // https://lab.isaaclin.cn/nCoV/api/area?latest=1
fetch( fetch(
'https://gw.alipayobjects.com/os/bmw-prod/0676f102-22f6-4c75-ab12-1ae200834b1c.json', 'https://gw.alipayobjects.com/os/bmw-prod/55a7dd2e-3fb4-4442-8899-900bb03ee67a.json',
).then((d) => d.json()), ).then((d) => d.json()),
]); ]);
const worldData = joinData(geoData, ncovData.results); const worldData = joinData(geoData, ncovData.results);

View File

@ -88,7 +88,7 @@ const World = React.memo(function Map() {
).then((d) => d.json()), ).then((d) => d.json()),
// https://lab.isaaclin.cn/nCoV/api/area?latest=1 // https://lab.isaaclin.cn/nCoV/api/area?latest=1
fetch( fetch(
'https://gw.alipayobjects.com/os/bmw-prod/0676f102-22f6-4c75-ab12-1ae200834b1c.json', 'https://gw.alipayobjects.com/os/bmw-prod/55a7dd2e-3fb4-4442-8899-900bb03ee67a.json',
).then((d) => d.json()), ).then((d) => d.json()),
]); ]);
const worldData = joinData(geoData, ncovData.results); const worldData = joinData(geoData, ncovData.results);

View File

@ -73,7 +73,7 @@ const World = React.memo(function Map() {
).then((d) => d.json()), ).then((d) => d.json()),
// https://lab.isaaclin.cn/nCoV/api/area?latest=1 // https://lab.isaaclin.cn/nCoV/api/area?latest=1
fetch( fetch(
'https://gw.alipayobjects.com/os/bmw-prod/0676f102-22f6-4c75-ab12-1ae200834b1c.json', 'https://gw.alipayobjects.com/os/bmw-prod/55a7dd2e-3fb4-4442-8899-900bb03ee67a.json',
).then((d) => d.json()), ).then((d) => d.json()),
]); ]);
const worldData = joinData(geoData, ncovData.results); const worldData = joinData(geoData, ncovData.results);

View File

@ -72,7 +72,7 @@ const World = React.memo(function Map() {
).then((d) => d.json()), ).then((d) => d.json()),
// https://lab.isaaclin.cn/nCoV/api/area?latest=1 // https://lab.isaaclin.cn/nCoV/api/area?latest=1
fetch( fetch(
'https://gw.alipayobjects.com/os/bmw-prod/0676f102-22f6-4c75-ab12-1ae200834b1c.json', 'https://gw.alipayobjects.com/os/bmw-prod/55a7dd2e-3fb4-4442-8899-900bb03ee67a.json',
).then((d) => d.json()), ).then((d) => d.json()),
]); ]);
setData(joinData(geoData, ncovData.results)); setData(joinData(geoData, ncovData.results));

View File

@ -73,7 +73,7 @@ const World = React.memo(function Map() {
).then((d) => d.json()), ).then((d) => d.json()),
// https://lab.isaaclin.cn/nCoV/api/area?latest=1 // https://lab.isaaclin.cn/nCoV/api/area?latest=1
fetch( fetch(
'https://gw.alipayobjects.com/os/bmw-prod/0676f102-22f6-4c75-ab12-1ae200834b1c.json', 'https://gw.alipayobjects.com/os/bmw-prod/55a7dd2e-3fb4-4442-8899-900bb03ee67a.json',
).then((d) => d.json()), ).then((d) => d.json()),
]); ]);
setData(joinData(geoData, ncovData.results)); setData(joinData(geoData, ncovData.results));

View File

@ -82,7 +82,7 @@ const World = React.memo(function Map() {
).then((d) => d.json()), ).then((d) => d.json()),
// https://lab.isaaclin.cn/nCoV/api/area?latest=1 // https://lab.isaaclin.cn/nCoV/api/area?latest=1
fetch( fetch(
'https://gw.alipayobjects.com/os/bmw-prod/0676f102-22f6-4c75-ab12-1ae200834b1c.json', 'https://gw.alipayobjects.com/os/bmw-prod/55a7dd2e-3fb4-4442-8899-900bb03ee67a.json',
).then((d) => d.json()), ).then((d) => d.json()),
fetch( fetch(
'https://gw.alipayobjects.com/os/bmw-prod/8990e8b4-c58e-419b-afb9-8ea3daff2dd1.json', 'https://gw.alipayobjects.com/os/bmw-prod/8990e8b4-c58e-419b-afb9-8ea3daff2dd1.json',

View File

@ -59,7 +59,6 @@ export default class InteractionService extends EventEmitter
private addEventListenerOnMap() { private addEventListenerOnMap() {
const $containter = this.mapService.getMapContainer(); const $containter = this.mapService.getMapContainer();
Hammer.defaults.domEvents = true;
if ($containter) { if ($containter) {
const hammertime = new Hammer.Manager($containter); const hammertime = new Hammer.Manager($containter);
hammertime.add( hammertime.add(

View File

@ -34,7 +34,7 @@ vec4 filterPickingColor(vec4 color) {
if (u_PickingStage == PICKING_ENCODE && length(pickingColor) < 0.001) { if (u_PickingStage == PICKING_ENCODE && length(pickingColor) < 0.001) {
discard; discard;
} }
return u_PickingStage == PICKING_ENCODE ? vec4(pickingColor, 1.0) : color; return u_PickingStage == PICKING_ENCODE ? vec4(pickingColor, step(0.001,color.a)): color;
} }
/* /*

View File

@ -0,0 +1,50 @@
import { Feature, Geometry, point, Properties } from '@turf/helpers';
import {
createCircle,
createPoint,
createPolygon,
createRect,
} from '../../src/util/create_geometry';
const circleData = {
type: 'Feature',
properties: {
units: 'kilometers',
steps: 4,
id: '1',
pointFeatures: [
{
type: 'Feature',
properties: {},
geometry: { type: 'Point', coordinates: [113, 40] },
},
],
active: true,
type: 'circle',
radius: 1115.6518229264275,
startPoint: { lng: 112, lat: 30 },
endPoint: { lng: 113, lat: 40 },
},
geometry: {
type: 'Polygon',
coordinates: [
[
[112, 40.03328403184138],
[100.45355724910682, 29.495378543228927],
[112, 19.966715968158613],
[123.54644275089319, 29.495378543228927],
[112, 40.03328403184138],
],
],
},
};
describe('creat_geometry', () => {
it('creatCircle', () => {
const circle = createCircle([112, 30], [113, 40], {
units: 'kilometers',
steps: 4,
id: '1',
pointFeatures: [point([113, 40])],
});
expect(circleData).toEqual(circle);
});
});

View File

@ -3,7 +3,7 @@
background: #fff; background: #fff;
box-shadow: 0 0 0 1px rgba(0,0,0,.1); box-shadow: 0 0 0 1px rgba(0,0,0,.1);
display: flex; display: flex;
a { button {
background-repeat: no-repeat; background-repeat: no-repeat;
background-position: center; background-position: center;
background-size: 20px 20px; background-size: 20px 20px;
@ -17,11 +17,11 @@
box-sizing: border-box; box-sizing: border-box;
cursor: pointer; cursor: pointer;
} }
a:not(:disabled):hover { button:not(:disabled):hover {
background-color: rgba(0, 0, 0, 0.05); background-color: rgba(0, 0, 0, 0.05);
} }
a:focus { button:focus {
box-shadow: 0 0 2px 2px #0096ff; box-shadow: 0 0 2px 2px #0096ff;
} }
.draw-point { .draw-point {
@ -46,25 +46,25 @@
} }
.horizontal { .horizontal {
flex-direction: row; flex-direction: row;
a:focus:first-child { button:focus:first-child {
border-radius: 4px 0px 0px 4px; border-radius: 4px 0px 0px 4px;
} }
a:focus:last-child { button:focus:last-child {
border-radius: 0px 4px 4px 4px; border-radius: 0px 4px 4px 4px;
} }
a+a { button+button {
border-left: 1px solid #eee; border-left: 1px solid #eee;
} }
} }
.vertical { .vertical {
flex-direction: column; flex-direction: column;
a:focus:first-child { button:focus:first-child {
border-radius: 4px 4px 0 0; border-radius: 4px 4px 0 0;
} }
a:focus:last-child { button:focus:last-child {
border-radius: 0px 0px 4px 4px; border-radius: 0px 0px 4px 4px;
} }
a+a { button+button {
border-top: 1px solid #eee; border-top: 1px solid #eee;
} }
} }

View File

@ -1,3 +1,9 @@
/*
* @Author: lzxue
* @Date: 2020-04-03 19:24:16
* @Last Modified by: mikey.zhaopeng
* @Last Modified time: 2020-04-03 19:30:39
*/
import { Control, IControlOption, PositionType, Scene } from '@antv/l7'; import { Control, IControlOption, PositionType, Scene } from '@antv/l7';
import { DOM } from '@antv/l7-utils'; import { DOM } from '@antv/l7-utils';
import './css/draw.less'; import './css/draw.less';
@ -10,6 +16,15 @@ import {
DrawPolygon, DrawPolygon,
DrawRect, DrawRect,
} from './modes'; } from './modes';
const DrawType: {
[key: string]: any;
} = {
point: DrawPoint,
line: DrawLine,
polygon: DrawPolygon,
circle: DrawCircle,
rect: DrawRect,
};
import { DrawEvent, DrawModes } from './util/constant'; import { DrawEvent, DrawModes } from './util/constant';
export interface IControls { export interface IControls {
[key: string]: boolean; [key: string]: boolean;
@ -47,64 +62,34 @@ export class DrawControl extends Control {
const controlClass = 'l7-control-draw' + ' ' + layout; const controlClass = 'l7-control-draw' + ' ' + layout;
const { controls } = this.controlOption as IDrawControlOption; const { controls } = this.controlOption as IDrawControlOption;
const container = DOM.create('div', controlClass) as HTMLElement; const container = DOM.create('div', controlClass) as HTMLElement;
if (controls.point) { this.addControls(controls, container);
this.draw.point = new DrawPoint(this.scene);
this.createButton(
'绘制点',
'draw-point',
container,
this.onButtonClick.bind(null, 'point'),
);
}
if (controls.line) {
this.draw.line = new DrawLine(this.scene);
this.createButton(
'绘制线',
'draw-line',
container,
this.onButtonClick.bind(null, 'line'),
);
}
if (controls.polygon) {
this.createButton(
'绘制面',
'draw-polygon',
container,
this.onButtonClick.bind(null, 'polygon'),
);
this.draw.polygon = new DrawPolygon(this.scene);
}
if (controls.rect) {
this.draw.rect = new DrawRect(this.scene);
this.createButton(
'绘制矩形',
'draw-rect',
container,
this.onButtonClick.bind(null, 'rect'),
);
}
if (controls.circle) {
this.draw.circle = new DrawCircle(this.scene);
this.createButton(
'绘制圆',
'draw-circle',
container,
this.onButtonClick.bind(null, 'circle'),
);
}
if (controls.delete) {
this.drawDelete = new DrawDelete(this.scene);
this.createButton(
'删除',
'draw-delete',
container,
this.onDeleteMode.bind(null, 'delete'),
);
}
// 监听组件 选中, 编辑 // 监听组件 选中, 编辑
return container; return container;
} }
private addControls(controls: IControls, container: HTMLElement) {
for (const type in controls) {
if (DrawType[type]) {
const draw = new DrawType[type](this.scene);
draw.on(DrawEvent.MODE_CHANGE, this.onModeChange.bind(null, type));
this.draw[type] = draw;
this.createButton(
draw.title,
'draw-' + type,
container,
this.onButtonClick.bind(null, type),
);
} else if (type === 'delete') {
const draw = new DrawDelete(this.scene);
draw.on(DrawEvent.MODE_CHANGE, this.onModeChange.bind(null, type));
this.createButton(
draw.title,
'draw-' + type,
container,
this.onDeleteMode.bind(null, type),
);
}
}
}
private createButton( private createButton(
tile: string, tile: string,
@ -112,10 +97,9 @@ export class DrawControl extends Control {
container: HTMLElement, container: HTMLElement,
fn: (...arg: any[]) => any, fn: (...arg: any[]) => any,
) { ) {
const link = DOM.create('a', className, container) as HTMLLinkElement; const link = DOM.create('button', className, container);
link.href = 'javascript:void(0)';
link.title = tile; link.title = tile;
link.addEventListener('mousedown', fn, true); link.addEventListener('mousedown', fn, false);
return link; return link;
} }
@ -124,7 +108,6 @@ export class DrawControl extends Control {
for (const draw in this.draw) { for (const draw in this.draw) {
if (draw === type) { if (draw === type) {
this.draw[draw].enable(); this.draw[draw].enable();
this.currentDraw = this.draw[draw];
} else { } else {
this.draw[draw].disable(); this.draw[draw].disable();
} }
@ -139,4 +122,10 @@ export class DrawControl extends Control {
this.currentDraw.deleteMode.enable(); this.currentDraw.deleteMode.enable();
return false; return false;
}; };
private onModeChange = (type: string, mode: string) => {
if (mode === DrawModes.SIMPLE_SELECT) {
this.currentDraw = this.draw[type];
}
};
} }

View File

@ -39,6 +39,13 @@ export default class DrawCircle extends DrawFeature {
this.source.setFeatureActive(feature); this.source.setFeatureActive(feature);
} }
protected getDefaultOptions() {
return {
...super.getDefaultOptions(),
title: '绘制圆',
};
}
protected onDragStart = (e: IInteractionTarget) => { protected onDragStart = (e: IInteractionTarget) => {
this.startPoint = e.lngLat; this.startPoint = e.lngLat;
this.setCursor('grabbing'); this.setCursor('grabbing');

View File

@ -21,6 +21,12 @@ export default class DrawDelete extends DrawFeature {
public disable() { public disable() {
return null; return null;
} }
protected getDefaultOptions() {
return {
...super.getDefaultOptions(),
title: '删除图形',
};
}
protected onDragStart(e: any): void { protected onDragStart(e: any): void {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');

View File

@ -14,6 +14,13 @@ export default class DrawLine extends DrawPolygon {
super(scene, options); super(scene, options);
this.type = 'line'; this.type = 'line';
} }
protected getDefaultOptions() {
return {
...super.getDefaultOptions(),
title: '绘制线',
};
}
protected moveFeature(delta: ILngLat): Feature { protected moveFeature(delta: ILngLat): Feature {
const newFeature = moveFeatures([this.currentFeature as Feature], delta); const newFeature = moveFeatures([this.currentFeature as Feature], delta);
const newPointFeture = moveFeatures(this.pointFeatures, delta); const newPointFeture = moveFeatures(this.pointFeatures, delta);

View File

@ -7,6 +7,7 @@ import LayerStyles from '../util/layerstyle';
export interface IDrawOption { export interface IDrawOption {
data: FeatureCollection; data: FeatureCollection;
title: string;
style: any; style: any;
} }
@ -23,6 +24,7 @@ export default abstract class DrawMode extends EventEmitter {
public source: DrawSource; public source: DrawSource;
public scene: Scene; public scene: Scene;
public type: string; public type: string;
public title: string;
public isEnable: boolean = false; public isEnable: boolean = false;
protected options: { protected options: {
@ -40,6 +42,7 @@ export default abstract class DrawMode extends EventEmitter {
this.scene = scene; this.scene = scene;
this.source = new DrawSource(data); this.source = new DrawSource(data);
this.options = merge(this.options, this.getDefaultOptions(), options); this.options = merge(this.options, this.getDefaultOptions(), options);
this.title = this.getOption('title');
} }
public enable() { public enable() {
if (this.isEnable) { if (this.isEnable) {

View File

@ -19,13 +19,19 @@ export default class DrawPoint extends DrawFeature {
super(scene, options); super(scene, options);
this.type = 'point'; this.type = 'point';
} }
public drawFinish() { public drawFinish() {
this.emit(DrawEvent.CREATE, this.currentFeature); this.emit(DrawEvent.CREATE, this.currentFeature);
this.emit(DrawEvent.MODE_CHANGE, DrawModes.SIMPLE_SELECT); this.emit(DrawEvent.MODE_CHANGE, DrawModes.SIMPLE_SELECT);
this.disable(); this.disable();
} }
protected getDefaultOptions() {
return {
...super.getDefaultOptions(),
title: '绘制点',
};
}
protected onDragStart = (e: IInteractionTarget) => { protected onDragStart = (e: IInteractionTarget) => {
return null; return null;
}; };

View File

@ -91,6 +91,13 @@ export default class DrawPolygon extends DrawFeature {
feature.properties.pointFeatures = pointfeatures.features; feature.properties.pointFeatures = pointfeatures.features;
this.setCurrentFeature(feature); this.setCurrentFeature(feature);
} }
protected getDefaultOptions() {
return {
...super.getDefaultOptions(),
title: '绘制多边形',
};
}
protected onDragStart = (e: IInteractionTarget) => { protected onDragStart = (e: IInteractionTarget) => {
return null; return null;
}; };
@ -108,10 +115,10 @@ export default class DrawPolygon extends DrawFeature {
this.points.push(lngLat); this.points.push(lngLat);
const feature = this.createFeature(this.points); const feature = this.createFeature(this.points);
const properties = feature.properties as { pointFeatures: Feature[] }; const properties = feature.properties as { pointFeatures: Feature[] };
// const pointfeatures = createPoint([this.points[0], this.endPoint]); const pointfeatures = createPoint([this.points[0], this.endPoint]);
// this.pointFeatures = pointfeatures.features; // this.pointFeatures = pointfeatures.features;
this.drawRender.update(featureCollection([feature])); this.drawRender.update(featureCollection([feature]));
this.drawVertexLayer.update(featureCollection(properties.pointFeatures)); this.drawVertexLayer.update(featureCollection(pointfeatures.features));
this.onDraw(); this.onDraw();
}; };

View File

@ -21,6 +21,12 @@ export default class DrawRect extends DrawCircle {
public drawFinish() { public drawFinish() {
return null; return null;
} }
protected getDefaultOptions() {
return {
...super.getDefaultOptions(),
title: '绘制矩形',
};
}
protected createFeature(id: string = '0'): Feature { protected createFeature(id: string = '0'): Feature {
const points = createPoint([this.endPoint]); const points = createPoint([this.endPoint]);

View File

@ -36,7 +36,6 @@ export function createCircle(
lng: endPoint[0], lng: endPoint[0],
lat: endPoint[1], lat: endPoint[1],
}, },
path: [center, endPoint],
}, },
}); });
return feature as Feature; return feature as Feature;

View File

@ -24,11 +24,11 @@
"author": "antv", "author": "antv",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@antv/l7-component": "^2.1.10", "@antv/l7-component": "2.1.10",
"@antv/l7-core": "^2.1.10", "@antv/l7-core": "2.1.10",
"@antv/l7-layers": "^2.1.10", "@antv/l7-layers": "2.1.10",
"@antv/l7-maps": "^2.1.10", "@antv/l7-maps": "2.1.10",
"@antv/l7-scene": "^2.1.10", "@antv/l7-scene": "2.1.10",
"@babel/runtime": "^7.7.7" "@babel/runtime": "^7.7.7"
}, },
"gitHead": "a5d354b66873f700730248d015c5e539c54b34b7", "gitHead": "a5d354b66873f700730248d015c5e539c54b34b7",

View File

@ -25,8 +25,8 @@
"author": "xiaoiver", "author": "xiaoiver",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@antv/l7-core": "^2.1.10", "@antv/l7-core": "2.1.10",
"@antv/l7-utils": "^2.1.10", "@antv/l7-utils": "2.1.10",
"@babel/runtime": "^7.7.7", "@babel/runtime": "^7.7.7",
"@types/amap-js-api": "^1.4.6", "@types/amap-js-api": "^1.4.6",
"gl-matrix": "^3.1.0", "gl-matrix": "^3.1.0",

View File

@ -73,9 +73,8 @@ export default class MarkerComponent extends React.Component {
scene.on('loaded', () => { scene.on('loaded', () => {
// @ts-ignore // @ts-ignore
marker.on('click', (e) => { // marker.on('click', (e) => {
console.log(this.scene.getZoom()); // });
});
// const marker1 = new AMap.Marker({ // const marker1 = new AMap.Marker({
// map: scene.map, // map: scene.map,
// position: [120.184824, 30.248341], // position: [120.184824, 30.248341],

View File

@ -37,7 +37,9 @@ export default class Point3D extends React.Component {
}) })
.shape('circle') .shape('circle')
.size(8) .size(8)
.active(true) .active({
color: 'red',
})
.color('Magnitude', [ .color('Magnitude', [
'#0A3663', '#0A3663',
'#1558AC', '#1558AC',