Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 2 months 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 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-20T17:02:33.000Z (about 4 years ago)
- Last Synced: 2024-06-20T10:16:03.528Z (7 months 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 mainimport (
"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 mainimport (
"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 mainimport (
"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()
}
```