https://github.com/abrander/gowasm-geolocation
Go package for the browser's geolocation API
https://github.com/abrander/gowasm-geolocation
geolocation-api go golang wasm
Last synced: about 1 year ago
JSON representation
Go package for the browser's geolocation API
- Host: GitHub
- URL: https://github.com/abrander/gowasm-geolocation
- Owner: abrander
- License: mit
- Created: 2020-12-19T08:13:17.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-20T17:02:33.000Z (about 5 years ago)
- Last Synced: 2025-01-15T09:00:00.348Z (about 1 year ago)
- Topics: geolocation-api, go, golang, wasm
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gowasm-geolocation
Geolocation is an idiomatic and intuitive Go package for using
the browser's geolocation API.
[![GoDoc][1]][2]
[1]: https://godoc.org/github.com/abrander/gowasm-geolocation?status.svg
[2]: https://godoc.org/github.com/abrander/gowasm-geolocation
## Examples
### Getting the device position
```go
package main
import (
"fmt"
"github.com/abrander/gowe/geolocation"
)
func main() {
options := &geolocation.PositionOptions{
HighAccuracy: true,
}
pos, err := geolocation.CurrentPosition(options)
if err != nil {
fmt.Printf("Ohh no: %s\n", err.Error())
}
fmt.Printf("Got position at %s: %+v\n", pos.Timestamp.String(), pos.Coords)
}
```
### Watching the device position
```go
package main
import (
"fmt"
"github.com/abrander/gowe/geolocation"
)
func main() {
w := geolocation.WatchPosition(nil)
for {
pos, err := w.Next()
if err != nil {
if err.Temporary() {
continue
}
fmt.Printf("Something went wrong: %s\n", err.Error())
break
}
fmt.Printf("Got new position at %s: %+v\n", pos.Timestamp.String(), pos.Coords)
}
w.Close()
}
```
### Integrate in a select style main loop
```go
package main
import (
"fmt"
"time"
"github.com/abrander/gowe/geolocation"
)
func main() {
options := &geolocation.PositionOptions{
MaximumAge: 10 * time.Second,
}
w := geolocation.WatchPosition(options)
positions, locationErrors := w.Chans()
MAIN:
for {
select {
case pos := <-positions:
fmt.Printf("Got position at %s: %+v\n", pos.Timestamp.String(), pos.Coords)
case err := <-locationErrors:
fmt.Printf("Ohh no: %s\n", err.Error())
if !err.Temporary() {
break MAIN
}
}
}
w.Close()
}
```