Skip to content

abrander/gowasm-geolocation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gowasm-geolocation

Geolocation is an idiomatic and intuitive Go package for using the browser's geolocation API.

GoDoc

Examples

Getting the device position

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

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

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()
}

About

Go package for the browser's geolocation API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages