Skip to content

Commit

Permalink
Geometry fire positionchange when altitude update (#2355)
Browse files Browse the repository at this point in the history
* Geometry fire positionchange when altitude update

* fix infowindow position error when owner altitude change

* spec

* updates

* clear geometry altitude cache when layer enableAltitude change

* spec

* updates

* fix lint
  • Loading branch information
deyihu authored Jun 19, 2024
1 parent c245682 commit cf102c7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/geometry/Geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
const layerOpts = layer.options;
const layerAltitude = layer.getAltitude ? layer.getAltitude() : 0;
const enableAltitude = layerOpts['enableAltitude'];
if (!enableAltitude) {
if (!enableAltitude && (layer as any).isVectorLayer) {
return layerAltitude;
}
const altitudeProperty = getAltitudeProperty(layer);
Expand Down Expand Up @@ -1779,6 +1779,7 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
}
}
this._clearAltitudeCache();
this.onPositionChanged();
return this;
}

Expand Down Expand Up @@ -1834,6 +1835,10 @@ export type GeometryOptionsType = {
function getAltitudeProperty(layer: OverlayLayer): string {
let altitudeProperty = 'altitude';
if (layer) {
//only VectorLayer support properties.altitude
if (!(layer as any).isVectorLayer) {
return null;
}
const layerOpts = layer.options;
altitudeProperty = layerOpts['altitudeProperty'];
}
Expand Down
12 changes: 12 additions & 0 deletions src/layer/VectorLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const options: VectorLayerOptionsType = {
class VectorLayer extends OverlayLayer {

options: VectorLayerOptionsType;
isVectorLayer: boolean;
/**
* @param id - layer's id
* @param geometries=null - geometries to add
Expand All @@ -91,10 +92,21 @@ class VectorLayer extends OverlayLayer {
*/
constructor(id: string, geometries: VectorLayerOptionsType | Array<Geometry>, options?: VectorLayerOptionsType) {
super(id, geometries, options);
this.isVectorLayer = true;
}

onConfig(conf: Record<string, any>) {
super.onConfig(conf);
if (!isNil(conf['enableAltitude'])) {
const geos = this.getGeometries() || [];
for (let i = 0, len = geos.length; i < len; i++) {
const geo = geos[i];
if (geo) {
geo._clearAltitudeCache();
geo.fire('positionchange');
}
}
}
if (conf['enableAltitude'] || conf['drawAltitude'] || conf['altitudeProperty']) {
const renderer = this.getRenderer();
if (renderer && renderer.setToRedraw) {
Expand Down
20 changes: 19 additions & 1 deletion src/ui/UIComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,16 @@ class UIComponent extends Eventable(Class) {
altitude = 0;
}
}
if (this._owner.getLayer) {
const layer = (this._owner as Geometry).getLayer();
//VectorLayer
if (layer && (layer as any).isVectorLayer) {
altitude = (this._owner as Geometry)._getAltitude() as number || 0;
if (!isNumber(altitude)) {
altitude = 0;
}
}
}
const alt = this._meterToPoint(this._coordinate, altitude);
return this.getMap().coordToViewPoint(this._coordinate, undefined, alt)
._add(this.options['dx'], this.options['dy']);
Expand Down Expand Up @@ -820,7 +830,15 @@ class UIComponent extends Eventable(Class) {
onGeometryPositionChange(param) {
if (this._owner && this.isVisible()) {
this._showBySymbolChange = true;
this.show(param['target'].getCenter());
const target = param.target;
const center = target.getCenter();
if (target._getAltitude) {
const altitude = target._getAltitude();
if (isNumber(altitude)) {
center.z = altitude;
}
}
this.show(center);
delete this._showBySymbolChange;
}
}
Expand Down
34 changes: 34 additions & 0 deletions test/geometry/GeometryAltitudeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,38 @@ describe('Geometry.Altitude', function () {
}, 100);
});

it('#2354 fire positionchange event when altitude update', function (done) {
map.addLayer(layer);
const point = new maptalks.Marker(map.getCenter()).addTo(layer);

setTimeout(() => {
var spy = sinon.spy();
point.on('positionchange', spy);
point.setAltitude(100);
setTimeout(() => {
expect(spy.called).to.be.ok();
done();
}, 100);

}, 100);
});

it('#2354 fire positionchange event when layer enableAltitude change', function (done) {
map.addLayer(layer);
const point = new maptalks.Marker(map.getCenter()).addTo(layer);

setTimeout(() => {
var spy = sinon.spy();
point.on('positionchange', spy);
layer.config({ enableAltitude: false });
setTimeout(() => {
expect(spy.called).to.be.ok();
expect(point._minAlt).to.be.equal(undefined);
expect(point._maxAlt).to.be.equal(undefined);
done();
}, 100);

}, 100);
});

});
8 changes: 7 additions & 1 deletion test/geometry/TextBoxSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ describe('Geometry.TextBox', function () {
layer = new maptalks.VectorLayer('id', { 'drawImmediate': true });
map.addLayer(layer);
layer.addGeometry(vector);
expect(layer).to.be.painted(100 / 2 - padding[0] - 2, 0);
var parser = new UAParser();
var result = parser.getOS();
if (result.name && result.name.toLowerCase().indexOf('windows') > -1) {
expect(layer).to.be.painted(100 / 2 - padding[0] - 4, 0);
} else {
expect(layer).to.be.painted(100 / 2 - padding[0] - 2, 0);
}
expect(layer).not.to.be.painted(100 / 2 - padding[0] + 1, 0);
expect(layer).not.to.be.painted(0, 0);
});
Expand Down

0 comments on commit cf102c7

Please sign in to comment.