Skip to content

Commit

Permalink
Map: turn on 3D terrain when user tilts (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz authored Oct 7, 2023
1 parent 523fbd4 commit 410faaf
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/components/Map/behaviour/useToggleTerrainControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,43 @@
import maplibregl from 'maplibre-gl';
import { useAddMapEvent } from '../../helpers';

const TERRAIN = {
source: 'terrain',
exaggeration: 1,
};

const turnOnTerrain = (map) => {
map.setTerrain(TERRAIN);
map.setMaxPitch(85);
};

const turnOffTerrain = (map) => {
map.setTerrain(null);
map.setMaxPitch(60);
};

class OsmappTerrainControl extends maplibregl.TerrainControl {
_toggleTerrain = () => {
if (this._map.getTerrain()) {
this._map.setTerrain(null);
this._map.setMaxPitch(60);
turnOffTerrain(this._map);
} else {
this._map.setTerrain(this.options);
this._map.setMaxPitch(85);
turnOnTerrain(this._map);
}
this._updateTerrainIcon();
};
}

const terrainControl = new OsmappTerrainControl({
source: 'terrain',
exaggeration: 1,
});
const terrainControl = new OsmappTerrainControl(TERRAIN);

let added = false;

export const useToggleTerrainControl = useAddMapEvent((map) => ({
eventType: 'move',
eventHandler: () => {
if (map.getPitch() > 0) {
if (!added) {
map.addControl(terrainControl);
added = true;
}
} else if (added) {
map.removeControl(terrainControl);
added = false;
if (map.getPitch() > 0 && !added) {
map.addControl(terrainControl);
turnOnTerrain(map);
added = true;
}
},
}));

1 comment on commit 410faaf

@vercel
Copy link

@vercel vercel bot commented on 410faaf Oct 7, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

osmapp – ./

osmapp.vercel.app
osmapp-zbycz.vercel.app
osmapp-git-master-zbycz.vercel.app
osmapp.org

Please sign in to comment.