Skip to content

Commit

Permalink
add altitude for UIComponent.getPosition, fix #1121
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhenn committed Jun 18, 2020
1 parent af5295f commit 1fdf51f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
13 changes: 13 additions & 0 deletions src/geometry/Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,19 @@ class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
return properties;
}


//------------- altitude -------------
getAltitude() {
const layer = this.getLayer();
if (!layer) {
return 0;
}
const layerOpts = layer.options,
properties = this.getProperties();
const altitude = layerOpts['enableAltitude'] ? properties ? properties[layerOpts['altitudeProperty']] : 0 : 0;
return altitude;
}

}

Geometry.mergeOptions(options);
Expand Down
20 changes: 10 additions & 10 deletions src/map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,8 +1265,8 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
/**
* shorter alias for coordinateToViewPoint
*/
coordToViewPoint(coordinate, out) {
return this.coordinateToViewPoint(coordinate, out);
coordToViewPoint(coordinate, out, altitude) {
return this.coordinateToViewPoint(coordinate, out, altitude);
}

/**
Expand Down Expand Up @@ -2075,8 +2075,8 @@ Map.include(/** @lends Map.prototype */{
*/
coordinateToViewPoint: function () {
const COORD = new Coordinate(0, 0);
return function (coordinate, out) {
return this._prjToViewPoint(this.getProjection().project(coordinate, COORD), out);
return function (coordinate, out, altitude) {
return this._prjToViewPoint(this.getProjection().project(coordinate, COORD), out, altitude);
};
}(),

Expand Down Expand Up @@ -2184,12 +2184,12 @@ Map.include(/** @lends Map.prototype */{
*/
distanceToPoint: function () {
const POINT = new Point(0, 0);
return function (xDist, yDist, zoom) {
return function (xDist, yDist, zoom, paramCenter) {
const projection = this.getProjection();
if (!projection) {
return null;
}
const center = this.getCenter(),
const center = paramCenter || this.getCenter(),
target = projection.locate(center, xDist, yDist);
const p0 = this.coordToPoint(center, zoom, POINT),
p1 = this.coordToPoint(target, zoom);
Expand Down Expand Up @@ -2368,8 +2368,8 @@ Map.include(/** @lends Map.prototype */{
*/
_prjToContainerPoint: function () {
const POINT = new Point(0, 0);
return function (pCoordinate, zoom, out) {
return this._pointToContainerPoint(this._prjToPoint(pCoordinate, zoom, POINT), zoom, 0, out);
return function (pCoordinate, zoom, out, altitude) {
return this._pointToContainerPoint(this._prjToPoint(pCoordinate, zoom, POINT), zoom, altitude || 0, out);
};
}(),

Expand All @@ -2382,8 +2382,8 @@ Map.include(/** @lends Map.prototype */{
*/
_prjToViewPoint: function () {
const POINT = new Point(0, 0);
return function (pCoordinate, out) {
const containerPoint = this._prjToContainerPoint(pCoordinate, undefined, POINT);
return function (pCoordinate, out, altitude) {
const containerPoint = this._prjToContainerPoint(pCoordinate, undefined, POINT, altitude);
return this.containerPointToViewPoint(containerPoint, out);
};
}(),
Expand Down
19 changes: 3 additions & 16 deletions src/renderer/geometry/Painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const registerSymbolizers = [
let testCanvas;

const TEMP_POINT0 = new Point(0, 0);
const TEMP_POINT1 = new Point(0, 0);
const TEMP_POINT2 = new Point(0, 0);
const TEMP_PAINT_EXTENT = new PointExtent();
const TEMP_EXTENT = new PointExtent();
const TEMP_FIXED_EXTENT = new PointExtent();
Expand Down Expand Up @@ -723,7 +721,7 @@ class Painter extends Class {
}

getAltitude() {
const propAlt = this._getAltitudeProperty();
const propAlt = this.geometry.getAltitude();
if (propAlt !== this._propAlt) {
this._altAtGLZoom = this._getGeometryAltitude();
}
Expand Down Expand Up @@ -752,7 +750,7 @@ class Painter extends Class {
if (!map) {
return 0;
}
const altitude = this._getAltitudeProperty();
const altitude = this.geometry.getAltitude();
this._propAlt = altitude;
if (!altitude) {
this.minAltitude = this.maxAltitude = 0;
Expand Down Expand Up @@ -784,18 +782,7 @@ class Painter extends Class {
_meterToPoint(center, altitude) {
const map = this.getMap();
const z = map.getGLZoom();
const target = map.locate(center, altitude, 0);
const p0 = map.coordToPoint(center, z, TEMP_POINT1),
p1 = map.coordToPoint(target, z, TEMP_POINT2);
return Math.abs(p1.x - p0.x) * sign(altitude);
}

_getAltitudeProperty() {
const geometry = this.geometry,
layerOpts = geometry.getLayer().options,
properties = geometry.getProperties();
const altitude = layerOpts['enableAltitude'] ? properties ? properties[layerOpts['altitudeProperty']] : 0 : 0;
return altitude;
return map.distanceToPoint(altitude, 0, z, center).x * sign(altitude);
}

_verifyProjection() {
Expand Down
14 changes: 12 additions & 2 deletions src/ui/UIComponent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend, isFunction } from '../core/util';
import { extend, isFunction, sign } from '../core/util';
import { trim } from '../core/util/strings';
import {
on,
Expand Down Expand Up @@ -383,10 +383,20 @@ class UIComponent extends Eventable(Class) {
}

_getViewPoint() {
return this.getMap().coordToViewPoint(this._coordinate)
let alt = 0;
if (this._owner && this._owner.getAltitude) {
const altitude = this._owner.getAltitude();
alt = this._meterToPoint(this._coordinate, altitude);
}
return this.getMap().coordToViewPoint(this._coordinate, undefined, alt)
._add(this.options['dx'], this.options['dy']);
}

_meterToPoint(center, altitude) {
const map = this.getMap();
return map.distanceToPoint(altitude, 0, undefined, center).x * sign(altitude);
}

_autoPan() {
const map = this.getMap(),
dom = this.getDOM();
Expand Down

0 comments on commit 1fdf51f

Please sign in to comment.