geolocation

package module
v0.0.0-...-d75ac79 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2020 License: MIT Imports: 3 Imported by: 0

README

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

Documentation

Overview

Package geolocation provides a convenient idiomatic wrapper for the browser geolocation API.

Index

Constants

View Source
const (
	// ErrUnknown will be returned if the value is not supplied by
	// the implementation.
	ErrUnknown = Error("value unknown")

	// ErrStationary will be returned if the device velocity is
	// requested when the device is stationary.
	ErrStationary = Error("device stationary")
)
View Source
const (
	// ErrPermissionDenied will be returned if the location
	// acquisition process failed because the document does
	// not have permission to use the Geolocation API.
	ErrPermissionDenied = Error("permission denied")

	// ErrPositionUnavailable will be returned if the position
	// of the device could not be determined. For instance, one
	// or more of the location providers used in the location
	// acquisition process reported an internal error that
	// caused the process to fail entirely.
	ErrPositionUnavailable = Error("position unavailable")

	// ErrTimeout will be returned if the length of time
	// specified by the Timeout property has elapsed before
	// the implementation could successfully acquire a new
	// GeolocationPosition object.
	ErrTimeout = Error("timeout")
)
View Source
const (
	// ErrWatcherClosed will be returned from Next if the watcher is closed.
	ErrWatcherClosed = Error("watcher closed")
)

Variables

This section is empty.

Functions

func CurrentPosition

func CurrentPosition(options *PositionOptions) (*Position, *Error)

CurrentPosition requests the location of the device.

Types

type Coords

type Coords struct {
	// Latitude and Longitude are geographic coordinates
	// specified in decimal degrees.
	Latitude  float64
	Longitude float64

	// Accuracy denotes the accuracy level of the latitude and
	// longitude coordinates in meters.
	Accuracy float64
	// contains filtered or unexported fields
}

Coords represents a place on Earth. The geographic coordinate reference system used is the World Geodetic System (2d) [WGS84].

func (*Coords) Altitude

func (c *Coords) Altitude() (float64, error)

Altitude denotes the height of the position, specified in meters above the [WGS84] ellipsoid. Altitude may not be avilable on all platforms at all times. ErrUnknown will be returned, if the altitude is not known or unsupported by the host device.

func (*Coords) AltitudeAccuracy

func (c *Coords) AltitudeAccuracy() (float64, error)

AltitudeAccuracy is specified in meters. This may return an ErrUnknown error if the accuracy is unavailable og unsupported on the host device.

func (*Coords) Heading

func (c *Coords) Heading() (float64, error)

Heading is the direction of travel of the device and is specified in degrees, where 0° ≤ Heading < 360°, counting clockwise relative to the true north. If this is unknown or unsupported, it will return an ErrUnknown error. If the host device is not moving, it will return ErrStationary.

func (*Coords) Speed

func (c *Coords) Speed() (float64, error)

Speed is the magnitude of the horizontal component of the hosting device's current velocity and is specified in meters per second. If the velocity is not known or unsupported, an ErrUnknown error will be returned.

type Error

type Error string

Error is an error type specific for this package. It satisfiers the error interface so you may pass it along as a regular error.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

func (Error) Temporary

func (e Error) Temporary() bool

Temporary returns true if the error is temporary and the operation can be retried.

type Position

type Position struct {
	Coords *Coords

	// Timestamp is the acquisition time of the position.
	Timestamp time.Time
}

Position is a container for the geolocation information.

type PositionOptions

type PositionOptions struct {
	// HighAccuracy provides a hint that the application would like
	// to receive the best possible results. This may result in
	// slower response times or increased power consumption. The
	// user might also deny this capability, or the device might
	// not be able to provide more accurate results than if the
	// flag wasn't specified.
	HighAccuracy bool

	// Timeout denotes the maximum length of time that is allowed
	// to pass until a position or an error is returned.
	Timeout time.Duration

	// MaximumAge indicates that the application is willing to
	// accept a cached position whose age is no greater than the
	// specified time.
	MaximumAge time.Duration
}

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher provides a convenient interface for watching the device position.

func WatchPosition

func WatchPosition(options *PositionOptions) *Watcher

WatchPosition will continually watch the device position.

func (Watcher) Chans

func (w Watcher) Chans() (chan *Position, chan Error)

Chans returns two channels. One carrying location updates, and the other carrying errors. This is useful for getting updates in a select loop.

func (*Watcher) Close

func (w *Watcher) Close()

Close unregisters the watcher with the host device.

func (*Watcher) Next

func (w *Watcher) Next() (*Position, *Error)

Next will return a position or an error. The error should be checked using Temporary(). If Temporary() returns true, the operation may be retried. If it returns false, you must consider the watcher closed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL