Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Go to current location #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This utility helps simulate the color schemes quickly, along with clipping & map
- [ ] Shape file through uploads (from local system)
- [X] Format available - TopoJson
- [ ] More formats required (Geojson etc)
- [ ] Auto fit to shape file location (on file load)
- [X] Auto fit to shape file location (on file load)
- [ ] Color selector
- [X] Add colors
- [X] Remove colors
Expand All @@ -30,7 +30,7 @@ This utility helps simulate the color schemes quickly, along with clipping & map
- [ ] Download color format for ASE, GIMP/Inscape color scheme
- [ ] Mobile version
- [ ] Search bar to search places
- [ ] Go to current location
- [X] Go to current location

## Installation
clone repo and install using -
Expand Down
10 changes: 10 additions & 0 deletions src/components/main-app/ol-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'ol/ol.css';
import { URL_SHAPE, URL_TILES, URL_COLORS, URL_OPACITY } from 'config';
import { useQueryParam, StringParam } from 'use-query-params';
import { usePrevious } from 'react-use';
import { unByKey } from 'ol/Observable';

export default function OlInit() {
const [shape] = useQueryParam(URL_SHAPE, StringParam);
Expand All @@ -25,6 +26,15 @@ export default function OlInit() {
if (olInstances.shapeSource && shape && prevShape !== shape) {
olInstances.shapeSource.setUrl(shape);
olInstances.shapeSource.refresh();

let source = olInstances.shapeSource;
var listenerKey = source.on('change', function (e) {
if (source.getState() === 'ready') {
var extent = source.getExtent();
olInstances.map.getView().fit(extent, { padding: [20, 20, 20, 420] });
unByKey(listenerKey);
}
});
}

if (olInstances.rasterLayer) {
Expand Down
14 changes: 13 additions & 1 deletion src/components/main-app/ol/init-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ import Map from 'ol/Map';
import XYZ from 'ol/source/XYZ';
import { Tile as TileLayer } from 'ol/layer';
import View from 'ol/View';
import { Control, defaults as defaultControls } from 'ol/control';

export default function initMap({
rasterLayer,
clipLayer,
boundaryLayer,
position,
}) {
var element = document.createElement('div');
element.innerHTML = '<i class="fa fa-map-marker iconButton"></i>';
element.className = 'current-location-button';
element.addEventListener('click', position);

export default function initMap({ rasterLayer, clipLayer, boundaryLayer }) {
return new Map({
target: 'map',
controls: defaultControls().extend([new Control({ element })]),
view: new View({ center: [0, 0], zoom: 0 }),
layers: [
new TileLayer({
Expand Down
20 changes: 19 additions & 1 deletion src/components/main-app/ol/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { clipRasterLayer, dragMap, initMap, loadRasterLayer } from './';
import { getColorsArray, getRgbColorsArray } from 'utils';
import { fromLonLat } from 'ol/proj';

let map = null;
let rasterSource = null;
Expand Down Expand Up @@ -30,6 +31,23 @@ export default function olMain({ shape, tiles, colors, opacity }) {
tiles,
});

const position = async () => {
try {
if (map) {
await navigator.geolocation.getCurrentPosition(position => {
map
.getView()
.setCenter(
fromLonLat([position.coords.longitude, position.coords.latitude])
);
map.getView().setZoom(11);
});
}
} catch (error) {
throw new Error(error);
}
};

rasterLayer = loadRaster.rasterLayer;
rasterSource = loadRaster.rasterSource;
rasterColorSource = loadRaster.rasterColorSource;
Expand All @@ -39,7 +57,7 @@ export default function olMain({ shape, tiles, colors, opacity }) {
const boundaryLayer = clipRaster.boundaryLayer;
shapeSource = clipRaster.shapeSource;

map = initMap({ rasterLayer, clipLayer, boundaryLayer });
map = initMap({ rasterLayer, clipLayer, boundaryLayer, position });
dragMap(map);
return { map, rasterSource, rasterLayer, shapeSource, rasterColorSource };
}
11 changes: 10 additions & 1 deletion src/sass/_app.sass
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
font-size: 15px
border-radius: 6px

.current-location-button
position: absolute
bottom: 20px
right: 20px

@media only screen and (max-width: 1440px)
.sidebar
Expand All @@ -99,4 +103,9 @@

@media only screen and (max-width: 1280px)
.sidebar
width: 360px
width: 360px

@media only screen and (max-width: 769px)
.current-location-button
top: 150px
right: 14px