Skip to content

Commit 95ba6d8

Browse files
committed
use transform's elevation in setLocationAtPoint()
1 parent ba54703 commit 95ba6d8

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/geo/projection/mercator_transform.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('transform', () => {
5353
expect([...transform.modelViewProjectionMatrix.values()]).toEqual([3, 0, 0, 0, 0, -2.954423259036624, -0.1780177690666898, -0.17364817766693033, -0, 0.006822967915294533, -0.013222891287479163, -0.012898324631281611, -786432, 774484.3308168967, 47414.91102496082, 46270.827886319785]);
5454
expect(fixedLngLat(transform.screenPointToLocation(new Point(250, 250)))).toEqual({lng: 0, lat: 0});
5555
expect(fixedCoord(transform.screenPointToMercatorCoordinate(new Point(250, 250)))).toEqual({x: 0.5, y: 0.5, z: 0});
56+
expect(fixedCoord(transform.screenPointToMercatorCoordinateAtZ(new Point(250, 250), 1))).toEqual({x: 0.5, y: 0.5000000044, z: 1});
5657
expect(transform.locationToScreenPoint(new LngLat(0, 0))).toEqual({x: 250, y: 250});
5758
});
5859

@@ -660,8 +661,6 @@ describe('transform', () => {
660661
transform.setElevation(centerInfo.elevation);
661662
transform.setBearing(bearing);
662663
transform.setPitch(pitch);
663-
console.log(centerInfo);
664-
console.log(transform);
665664
expect(transform.zoom).toBeGreaterThan(0);
666665
expect(transform.getCameraAltitude()).toBeCloseTo(camAlt, 10);
667666
expect(transform.getCameraLngLat().lng).toBeCloseTo(camLngLat.lng, 10);

src/geo/projection/mercator_transform.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@ export class MercatorTransform implements ITransform {
297297
}
298298

299299
setLocationAtPoint(lnglat: LngLat, point: Point) {
300-
const a = this.screenPointToMercatorCoordinate(point);
301-
const b = this.screenPointToMercatorCoordinate(this.centerPoint);
300+
const z = mercatorZfromAltitude(this.elevation, this.center.lat);
301+
const a = this.screenPointToMercatorCoordinateAtZ(point, z);
302+
const b = this.screenPointToMercatorCoordinateAtZ(this.centerPoint, z);
302303
const loc = locationToMercatorCoordinate(lnglat);
303304
const newCenter = new MercatorCoordinate(
304305
loc.x - (a.x - b.x),
@@ -327,9 +328,13 @@ export class MercatorTransform implements ITransform {
327328
return coordinate;
328329
}
329330
}
331+
return this.screenPointToMercatorCoordinateAtZ(p);
332+
}
333+
334+
screenPointToMercatorCoordinateAtZ(p: Point, mercatorZ?: number): MercatorCoordinate {
330335

331336
// calculate point-coordinate on flat earth
332-
const targetZ = 0;
337+
const targetZ = mercatorZ ? mercatorZ : 0;
333338
// since we don't know the correct projected z value for the point,
334339
// unproject two points to get a line and then find the point on that
335340
// line with z=0
@@ -353,7 +358,8 @@ export class MercatorTransform implements ITransform {
353358

354359
return new MercatorCoordinate(
355360
interpolates.number(x0, x1, t) / this.worldSize,
356-
interpolates.number(y0, y1, t) / this.worldSize);
361+
interpolates.number(y0, y1, t) / this.worldSize,
362+
targetZ);
357363
}
358364

359365
/**

0 commit comments

Comments
 (0)