Skip to content

Commit

Permalink
Merge pull request #12 from qazsato/remove_accuracy
Browse files Browse the repository at this point in the history
remove accuracy option
  • Loading branch information
qazsato authored Dec 3, 2024
2 parents cce09c1 + 44e3d9f commit 411ac39
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 46 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import 'maplibre-gl-compass/style.css'
const map = new Map({/* YOUR_MAP_OPTION */})

const compass = new CompassControl({
accuracy: 10.0, // If the accuracy is lower than this value, the map bearing will not be updated. Default is not set.
debug: true, // Show debug view. Default is false.
timeout: 10000, // The maximum time to wait for a DeviceOrientationEvent. Default is 3000 [ms].
visible: true, // Show compass button. Default is true.
Expand All @@ -73,12 +72,11 @@ map.addControl(compass)

## Options

| name | default | description |
| -------- | ------- | ------------------------------------------------------------------------------ |
| accuracy | | If the accuracy is lower than this value, the map bearing will not be updated. |
| debug | false | Show debug view. |
| timeout | 3000 | The maximum time[ms] to wait for a DeviceOrientationEvent. |
| visible | true | Show compass button. |
| name | default | description |
| ------- | ------- | ---------------------------------------------------------- |
| debug | false | Show debug view. |
| timeout | 3000 | The maximum time[ms] to wait for a DeviceOrientationEvent. |
| visible | true | Show compass button. |

## Events

Expand Down
11 changes: 5 additions & 6 deletions docs/index-Dgo8Clwm.js → docs/index-CarpIPlA.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/maplibre-gl-compass/compass.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>maplibre-gl-compass</title>
<script type="module" crossorigin src="/maplibre-gl-compass/index-Dgo8Clwm.js"></script>
<script type="module" crossorigin src="/maplibre-gl-compass/index-CarpIPlA.js"></script>
<link rel="stylesheet" crossorigin href="/maplibre-gl-compass/index-oS4W3OgK.css">
</head>
<body>
Expand Down
7 changes: 1 addition & 6 deletions src/components/DebugView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ export class DebugView {
this.element.innerHTML = `
<ul class="maplibregl-ctrl-compass-heading-debug">
<li><b>heading</b>: <span class="heading"></span></li>
<li><b>accuracy</b>: <span class="accuracy"></span></li>
</ul>
`
parent.appendChild(this.element)
}

update(heading: string, accuracy: string) {
update(heading: string) {
const headingSpan = this.element.querySelector('.heading')
const accuracySpan = this.element.querySelector('.accuracy')
if (headingSpan) {
headingSpan.textContent = heading
}
if (accuracySpan) {
accuracySpan.textContent = accuracy
}
}
}
15 changes: 2 additions & 13 deletions src/maplibre-gl-compass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { DebugView } from './components/DebugView'
// https://developer.mozilla.org/ja/docs/Web/API/DeviceOrientationEvent
export type WebkitDeviceOrientationEvent = DeviceOrientationEvent & {
webkitCompassHeading: number | undefined
webkitCompassAccuracy: number | undefined
}

type CompassControlOptions = {
accuracy?: number
timeout?: number
debug?: boolean
visible?: boolean
Expand All @@ -32,7 +30,6 @@ export class CompassControl implements IControl {

private active = false
private currentHeading: number | undefined
private currentAccuracy: number | undefined

private deviceorientationCallback:
| ((event: WebkitDeviceOrientationEvent) => void)
Expand Down Expand Up @@ -151,7 +148,6 @@ export class CompassControl implements IControl {
if (!this.map) return
const webkitEvent = event as WebkitDeviceOrientationEvent
this.currentHeading = webkitEvent.webkitCompassHeading
this.currentAccuracy = webkitEvent.webkitCompassAccuracy
if (this.deviceorientationCallback) {
this.deviceorientationCallback(webkitEvent)
}
Expand All @@ -162,25 +158,18 @@ export class CompassControl implements IControl {
return
}
const bearing = this.map.getBearing()
if (
this.options.accuracy &&
this.currentAccuracy &&
this.currentAccuracy < this.options.accuracy
) {
return
}
if (Math.abs(this.currentHeading - bearing) >= 1) {
this.map?.setBearing(this.currentHeading)
}
this.compassButton.stopLoading()
}

private updateDebugView() {
this.debugView?.update(`${this.currentHeading}`, `${this.currentAccuracy}`)
this.debugView?.update(`${this.currentHeading}`)
}

private clearDebugView() {
this.debugView?.update('', '')
this.debugView?.update('')
}

private disable() {
Expand Down
14 changes: 3 additions & 11 deletions tests/DebugView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,20 @@ describe('DebugView', () => {
).toBeTruthy()
})

it('should update heading and accuracy values', () => {
debugView.update('180.123', '10.789')
it('should update heading values', () => {
debugView.update('180.123')

const headingSpan = parentElement.querySelector('.heading')
const accuracySpan = parentElement.querySelector('.accuracy')

expect(headingSpan?.textContent).toBe('180.123')
expect(accuracySpan?.textContent).toBe('10.789')
})

it('should handle missing elements gracefully', () => {
const headingSpan = parentElement.querySelector('.heading')
const accuracySpan = parentElement.querySelector('.accuracy')
headingSpan?.remove()
accuracySpan?.remove()

const updatedHeadingSpan = parentElement.querySelector('.heading')
const updatedAccuracySpan = parentElement.querySelector('.accuracy')

expect(() => debugView.update('180.123', '10.789')).not.toThrow()

expect(() => debugView.update('180.123')).not.toThrow()
expect(updatedHeadingSpan).toBeNull()
expect(updatedAccuracySpan).toBeNull()
})
})
2 changes: 0 additions & 2 deletions tests/maplibre-gl-compass.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,11 @@ describe('CompassControl', () => {
it('should update debug view when debug option is enabled', () => {
const debugControl = new CompassControl({ debug: true })
debugControl['currentHeading'] = 45
debugControl['currentAccuracy'] = 10

debugControl['updateDebugView']()
const debugContainer = debugControl['debugView']?.['element']

expect(debugContainer?.querySelector('.heading')?.textContent).toBe('45')
expect(debugContainer?.querySelector('.accuracy')?.textContent).toBe('10')
})

it('should toggle state when clicked', () => {
Expand Down

0 comments on commit 411ac39

Please sign in to comment.