Skip to content

Commit 95a6093

Browse files
authored
Merge pull request #51 from cwhelchel/dev
Add zoom to position button
2 parents cc8bc3f + 86c35bb commit 95a6093

File tree

9 files changed

+177
-35
lines changed

9 files changed

+177
-35
lines changed

BugReportControl.js renamed to controls/BugReportControl.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ export class BugReportControl extends Control {
99
constructor(opt_options) {
1010
const options = opt_options || {};
1111

12-
//const button = document.createElement('button');
13-
14-
const link = document.createElement('a');
15-
link.innerHTML = '⚠️';
16-
link.title = 'Report error on Github';
17-
link.href = "https://github.com/cwhelchel/potamap.ol/issues";
18-
link.target = "_blank";
12+
const btn = document.createElement('button');
13+
btn.innerHTML = '<a title="Report error on Github" href="https://github.com/cwhelchel/potamap.ol/issues" target="_blank">⚠️</a>'
14+
btn.className = 'btn';
1915

2016
const element = document.createElement('div');
2117
element.className = 'bug-rpt-control ol-unselectable ol-control';
22-
element.appendChild(link);
18+
element.appendChild(btn);
2319

2420
super({
2521
element: element,
File renamed without changes.
File renamed without changes.

controls/ZoomToPosControl.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"use strict";
2+
3+
import { Control } from 'ol/control.js';
4+
import Geolocation from 'ol/Geolocation.js';
5+
6+
export class ZoomToPosControl extends Control {
7+
/**
8+
* @param {Object} [opt_options] Control options.
9+
*/
10+
constructor(opt_options) {
11+
const options = opt_options || {};
12+
13+
const btn = document.createElement('button');
14+
btn.innerHTML = '🎯'
15+
btn.className = 'btn';
16+
btn.title = 'Zoom to your position';
17+
18+
const element = document.createElement('div');
19+
element.className = 'zoom-pos-control ol-unselectable ol-control';
20+
element.appendChild(btn);
21+
22+
super({
23+
element: element,
24+
target: options.target,
25+
});
26+
27+
this.callback = opt_options;
28+
29+
btn.addEventListener('click', this.handleButtonClick.bind(this), false);
30+
}
31+
32+
handleButtonClick() {
33+
34+
if (this.callback)
35+
this.callback();
36+
}
37+
}

getGeolocationLayer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import Feature from 'ol/Feature';
66
import Point from 'ol/geom/Point';
77
import Geolocation from 'ol/Geolocation.js';
88

9+
export var currentPosition;
10+
911
/*
1012
Gets a layer to be added to the map with the browsers geolocation data.
1113
The layer will display a position point and an accuracy "ring".
1214
1315
projection should be from the maps view, ala: view.getProjection()
1416
*/
1517
export function getGeolocationLayer(projection) {
18+
//currentPosition = [-152.404, 61.3707];
1619

1720
const geolocation = new Geolocation({
1821
// enableHighAccuracy must be set to true to have the heading value.
@@ -48,6 +51,7 @@ export function getGeolocationLayer(projection) {
4851

4952
geolocation.on('change:position', function () {
5053
const coordinates = geolocation.getPosition();
54+
currentPosition = coordinates;
5155
positionFeature.setGeometry(coordinates ? new Point(coordinates) : null);
5256
});
5357

index.css

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,39 @@
4343
}
4444

4545
/*
46-
on click popup
46+
feature/shape onClick popover
4747
*/
4848

49-
.shapeProps {
49+
.popover-body {
50+
--bs-popover-body-padding-x: 0.75rem;
51+
--bs-popover-body-padding-y: 0.50rem;
52+
}
53+
54+
.shape-popover {
5055
font-size: 0.75em;
5156
}
5257

58+
.shape-popover .shape-name {
59+
text-align: center;
60+
font-size: 1.09em;
61+
margin-bottom: 3px;
62+
}
63+
64+
.shape-popover .shape-props {
65+
display: flex;
66+
flex-direction: row;
67+
flex-wrap: wrap;
68+
}
69+
70+
.shape-popover .shape-prop {
71+
margin: 0px 5px;
72+
padding: 0px 5px;
73+
border: 1px;
74+
border-style: dotted;
75+
border-radius: 5px;
76+
color: rgba(var(--bs-secondary-rgb), 0.75);
77+
}
78+
5379
.hoverTitle {
5480
margin-left: 30px;
5581
text-overflow: ellipsis;
@@ -104,49 +130,93 @@
104130

105131
.info-control {
106132
left: .5em;
133+
min-width: 24px;
107134
font-size: 1.0em;
135+
background-color: var(--ol-background-color);
108136
}
109137

110138
.bug-rpt-control {
111139
left: .5em;
112140
font-size: 1.0em;
141+
min-width: 24px;
113142
background-color: var(--ol-background-color);
114143
}
115144

116145
.toggle-layer-control {
117146
left: .5em;
118147
font-size: 1.0em;
119-
background-color: var(--ol-background-color);;
148+
min-width: 24px;
149+
background-color: var(--ol-background-color);
150+
}
151+
152+
.zoom-pos-control {
153+
left: .5em;
154+
font-size: 1.0em;
155+
min-width: 24px;
156+
background-color: var(--ol-background-color);
120157
}
121158

159+
122160
@media only screen and (max-width: 600px) {
161+
123162
/*
124163
PHONE SCREENS
125164
*/
126165
.info-control {
127-
top: 100px;
166+
/* top: 100px; */
167+
top: auto;
168+
bottom: 25px;
169+
left: 5px;
128170
}
171+
129172
.bug-rpt-control {
130-
top: 145px;
173+
/* top: 145px; */
174+
top: auto;
175+
left: auto;
176+
right: 5px;
177+
bottom: 25px;
131178
}
179+
132180
.toggle-layer-control {
133-
top: 175px;
181+
top: 100px;
182+
}
183+
184+
.zoom-pos-control {
185+
top: 145px;
186+
}
187+
188+
.footer span {
189+
text-wrap: nowrap;
190+
}
191+
192+
.footer-text-r {
193+
text-align: left;
134194
}
135195
}
136196

137197

138198
@media only screen and (min-width: 600px) {
199+
139200
/*
140201
MEDIUM SCREENS
141202
*/
142203
.info-control {
143-
top: 100px;
204+
top: auto;
205+
bottom: 45px;
206+
left: 45px;
144207
}
208+
145209
.bug-rpt-control {
146-
top: 145px;
210+
top: auto;
211+
bottom: 45px;
147212
}
213+
148214
.toggle-layer-control {
149-
top: 175px;
215+
top: 75px;
216+
}
217+
218+
.zoom-pos-control {
219+
top: 110px;
150220
}
151221
}
152222

@@ -156,13 +226,22 @@
156226
*/
157227

158228
.info-control {
159-
top: 75px;
229+
top: auto;
230+
bottom: 10px;
231+
left: 45px;
160232
}
233+
161234
.bug-rpt-control {
162-
top: 110px;
235+
top: auto;
236+
bottom: 10px;
163237
}
238+
164239
.toggle-layer-control {
165-
top: 145px;
240+
top: 75px;
241+
}
242+
243+
.zoom-pos-control {
244+
top: 110px;
166245
}
167246
}
168247

main.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import LayerSwitcher from 'ol-layerswitcher';
1515
import { defaultStyle, initLayers } from './BoundaryLayers.js'
1616
import StaticData from './StaticData.js'
1717
import { getParkLocation, getParkLastActx } from './PotaApi.js'
18-
import { getGeolocationLayer } from './getGeolocationLayer.js';
19-
import { InfoControl } from './InfoControl.js'
20-
import { BugReportControl } from './BugReportControl.js';
21-
import { TileLayerControl } from './TileLayerControl.js';
18+
import { currentPosition, getGeolocationLayer } from './getGeolocationLayer.js';
19+
import { InfoControl } from './controls/InfoControl.js'
20+
import { BugReportControl } from './controls/BugReportControl.js';
21+
import { TileLayerControl } from './controls/TileLayerControl.js';
22+
import { ZoomToPosControl } from './controls/ZoomToPosControl.js';
2223

2324
const selectStyle = new Style({
2425
fill: new Fill({
@@ -80,18 +81,17 @@ const map = new Map({
8081
title: 'Map',
8182
type: 'base',
8283
view: view,
83-
controls: defaultControls().extend([new InfoControl(), new BugReportControl(), new TileLayerControl(handleLayerSwitchCallback)])
84+
controls: defaultControls().extend([new InfoControl(), new BugReportControl(), new TileLayerControl(handleLayerSwitchCallback), new ZoomToPosControl(zoomToPosition)])
8485
});
8586

86-
8787
// add layer and source for GPS position
8888
const geolocLayer = getGeolocationLayer(view.getProjection());
8989

9090
map.addLayer(geolocLayer);
9191

9292
// add our layer switcher component
9393
var layerSwitcher = new LayerSwitcher({
94-
startActive: true,
94+
startActive: false,
9595
activationMode: 'click',
9696
groupSelectStyle: 'children',
9797
reverse: false // this logic is backwards-af
@@ -147,16 +147,23 @@ map.on('click', function (evt) {
147147
let name = f.get('NAME'); // should ALWAYS be there
148148
let title = f.get('TITLE'); // will be there for pota parks
149149
let res = "";
150+
let shapeTitle = '';
150151

151152
// from a shapefile. use its properties as they provide way more info
152153
if (title === undefined) {
153154
let p = f.getProperties();
154155
for (var property in p) {
155156
if (typeof (p[property]) == "string") {
156-
res += `${property} : ${p[property]} <br/>`;
157+
if (property ==="NAME") {
158+
shapeTitle = `<div class="shape-name">${p["NAME"]}</div>`;
159+
continue;
160+
}
161+
res += `<div class="shape-prop">${property} : ${p[property]}</div>`;
157162
}
158163
}
159-
res = `<span class="shapeProps">${res}</span>`
164+
165+
let propsDiv = `<div class="shape-props">${res}</div>`
166+
res = `<div class="shape-popover">${shapeTitle}${propsDiv}</div>`
160167
}
161168
else {
162169
// from POTA park markers. get and display POTA specific info
@@ -206,6 +213,15 @@ function showLocLayerGroup(inVal) {
206213
}
207214
}
208215

216+
$(document).ready( function() {
217+
if (localStorage.getItem('locSelectVal') !== undefined) {
218+
const x = localStorage.getItem('locSelectVal');
219+
console.log(x);
220+
$('#locSelect').val(x);
221+
showLocLayerGroup(x);
222+
}
223+
} );
224+
209225
$('#parkBtn').click(function () {
210226
const input = $('#parkTxt').val();
211227
let loc = getParkLocation(input);
@@ -223,6 +239,7 @@ $('#parkTxt').keypress(function (event) {
223239
});
224240

225241
$('#locSelect').on("change", function () {
242+
localStorage.setItem('locSelectVal', this.value);
226243
showLocLayerGroup(this.value);
227244
});
228245

@@ -235,6 +252,15 @@ function zoomToLocation(locId) {
235252
map.getView().animate({ zoom: zoom, center: fromLonLat([lon, lat]) });
236253
}
237254

255+
function zoomToPosition() {
256+
const coordinates = currentPosition;
257+
if (coordinates !== undefined && coordinates !== null) {
258+
let zoom = 10;
259+
let c = fromLonLat(coordinates);
260+
map.getView().animate({ zoom: zoom, center: c});
261+
}
262+
}
263+
238264
function scrollToLayGroupInPanel(locId) {
239265
var container = $("div.panel");
240266
var element = $("label:contains('" + locId + "')");

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"serve": "vite preview"
88
},
99
"devDependencies": {
10-
"vite": "^5.4.10"
10+
"vite": "^5.4.11"
1111
},
1212
"dependencies": {
1313
"ol": "latest",

0 commit comments

Comments
 (0)