Skip to content

Commit

Permalink
Add Automatic Precision to Lat/Lon Display
Browse files Browse the repository at this point in the history
As the zoom level changes, the user will be presented with
different levels of precision on the latitude and longitude.

refs: #304
  • Loading branch information
theduckylittle authored and klassenjs committed Feb 8, 2021
1 parent cd740d9 commit 62e8e5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/gm3/components/coordinate-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ export function formatCoordinates(projection, coords, defaultPrecision = 4) {
return coords.map(x => x.toFixed(precision)).join(', ');
}

/**
* Convert the zoom level to a number of digits for lat/lon
* formatting.
*
* @param {Number} zoom - The map's zoom level.
*
* @returns {Number} the number of digits.
*/
const zoomToPrecision = zoom => {
if (zoom > 18) {
return 6;
} else if (zoom > 9) {
return 4;
}
return 2;
};

/**
* CoordinateDisplay options
* - These options can be passed when adding the coordinate display to the app
Expand Down Expand Up @@ -158,9 +175,10 @@ export default class CoordinateDisplay extends React.Component {
return proj.transform(this.props.coords, map_proj, latlon_proj);
}

latlon(projection) {
latlon(projection, autoPrecision = true) {
const coords = this.getLatLonCoords();
return formatCoordinates(projection, [coords[1], coords[0]]);
const digits = autoPrecision ? zoomToPrecision(this.props.zoom) : 4;
return formatCoordinates(projection, [coords[1], coords[0]], digits);
}

getProjectionCoords(projection) {
Expand Down
3 changes: 2 additions & 1 deletion src/gm3/components/coordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import CoordinateDisplay from './coordinate-display';
const mapToProps = function(store) {
return {
coords: store.cursor.coords,
resolution: store.map.resolution
resolution: store.map.resolution,
zoom: store.map.zoom,
}
}

Expand Down

0 comments on commit 62e8e5b

Please sign in to comment.