Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/maptalks/src/geometry/Geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
//@internal
_layer: OverlayLayer;
//@internal
_drawToolLayer: OverlayLayer;
//@internal
_angle: number
//@internal
_pivot: Coordinate
Expand Down
19 changes: 15 additions & 4 deletions packages/maptalks/src/layer/DrawToolLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export default class DrawToolLayer extends OverlayLayer {
static lineLayerClazz: any;
static polygonLayerClazz: any;
//@internal
_markerLayer: any;
_markerLayer: OverlayLayer;
//@internal
_lineLayer: any;
_lineLayer: OverlayLayer;
//@internal
_polygonLayer: any;
_polygonLayer: OverlayLayer;

static setLayerClass(markerLayerClass, lineLayerClass, polygonLayerClass) {
DrawToolLayer.markerLayerClazz = markerLayerClass;
Expand All @@ -31,7 +31,7 @@ export default class DrawToolLayer extends OverlayLayer {
* @param options=null - construct options
* @param options.style=null - drawToolLayer's style
*/
constructor(id: string, geometries?: DrawToolLayerOptionsType | Array<Geometry>, options: DrawToolLayerOptionsType = {}) {
constructor(id: string, geometries?: DrawToolLayerOptionsType | Array<Geometry>, options: DrawToolLayerOptionsType = {}) {
if (geometries && (!isGeometry(geometries) && !Array.isArray(geometries) && GEOJSON_TYPES.indexOf((geometries as any).type) < 0)) {
options = geometries;
geometries = null;
Expand All @@ -47,6 +47,14 @@ export default class DrawToolLayer extends OverlayLayer {
}
}

_bindDrawToolLayer() {
const geoList = this._geoList || [];
geoList.forEach(geo => {
geo._drawToolLayer = this;
});
return this;
}

bringToFront() {
this._polygonLayer.bringToFront();
this._lineLayer.bringToFront();
Expand All @@ -59,6 +67,7 @@ export default class DrawToolLayer extends OverlayLayer {
geometries = [geometries];
}
pushIn(this._geoList, geometries);
this._bindDrawToolLayer();
for (let i = 0; i < geometries.length; i++) {
if (this._markerLayer.isVectorLayer) {
this._markerLayer.addGeometry(geometries[i]);
Expand All @@ -80,6 +89,7 @@ export default class DrawToolLayer extends OverlayLayer {
}
for (let i = 0; i < geometries.length; i++) {
this._geoList.splice(geometries[i] as any, 1);
delete geometries[i]._drawToolLayer;
if (this._markerLayer.isVectorLayer) {
this._markerLayer.removeGeometry(geometries[i]);
continue;
Expand All @@ -99,6 +109,7 @@ export default class DrawToolLayer extends OverlayLayer {
for (let i = 0; i < geometries.length; i++) {
if (geometries[i]) {
this._geoList.splice(geometries[i] as any, 1);
delete geometries[i]._drawToolLayer;
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions packages/maptalks/src/layer/OverlayLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class OverlayLayer extends Layer {
//@internal
_renderer: OverlayLayerCanvasRenderer;

isVectorLayer?: boolean;

constructor(id: string, geometries?: OverlayLayerOptionsType | Array<Geometry>, options?: OverlayLayerOptionsType) {
if (geometries && (!isGeometry(geometries) && !Array.isArray(geometries) && GEOJSON_TYPES.indexOf((geometries as any).type) < 0)) {
options = geometries;
Expand Down Expand Up @@ -119,12 +121,20 @@ class OverlayLayer extends Layer {
if (isNil(id) || id === '') {
return null;
}
if (!this._geoMap[id]) {
return null;
if (this._geoMap[id]) {
return this._geoMap[id];
}
return this._geoMap[id];
const geoList = this._geoList || [];
for (let i = 0, len = geoList.length; i < len; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为何会出现 id 不在 this._geoMap 里的情况?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drawtool layer的geometry不是直接加到 drawtool layer的,所以肯定获取不到

const geo = geoList[i];
if (geo && geo.getId() === id) {
return geo;
}
}
return null;
}


/**
* 获取所有geometries,如果提供 filter() 方法,则根据方法返回
*
Expand Down
2 changes: 1 addition & 1 deletion packages/maptalks/src/map/tool/DrawToolRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const polygonHooks: modeActionType = {

// geometry._setPrjCoordinates(prjCoords);
geometry.setCoordinates(coordinates);
const layer = geometry.getLayer();
const layer = geometry._drawToolLayer || geometry.getLayer();
if (layer) {
let polygon = layer.getGeometryById('polygon');
if (!polygon && prjCoords.length >= 3) {
Expand Down