billstclair/elm-geolocation/1.0.0

NOTE: the latest version is 1.1.0

PortFunnel.Geolocation

Find out about where a user’s device is located. Geolocation API.

Location

type alias Location =
{ latitude : Float
, longitude : Float
, accuracy : Float
, altitude : Maybe Altitude
, movement : Maybe Movement
, timestamp : Posix
}

All available details of the device's current location in the world.

  • latitude — the latitude in decimal degrees.
  • longitude — the longitude in decimal degrees.
  • accuracy — the accuracy of the latitude and longitude, expressed in meters.
  • altitude — altitude information, if available.
  • movement — information about how the device is moving, if available.
  • timestamp — the time that this location reading was taken in milliseconds.
type alias Altitude =
{ value : Float
, accuracy : Float
}

The altitude in meters relative to sea level is held in value. The accuracy field describes how accurate value is, also in meters.

= Static
| Moving
{ speed : Float
, degreesFromNorth : Float
}

Describes the motion of the device. If the device is not moving, this will just be Static. If the device is moving, you will see the speed in meters per second and the degreesFromNorth in degrees.

Note: The degreesFromNorth value goes clockwise: 0° represents true north, 90° is east, 180° is south, 270° is west, etc.

Subscribe to Changes

Enable receipt of changes as the browser device moves.

Stop receiving changes as the browser device moves.

Get Current Location

Return a message to send to receive a location now.

type Error
= PermissionDenied String
| LocationUnavailable String
| Timeout String

The now functions may fail for a variety of reasons.

* The user may reject the request to use their location.
* It may be impossible to get a location.
* If you set a timeout in the `Options` the request may just take too long.

In each case, the browser will provide a string with additional information.

Options

nowWith : Options -> Message

Return a message to send to receive a location now with options.

type alias Options =
{ enableHighAccuracy : Bool
, timeout : Maybe Int
, maximumAge : Maybe Int
}

There are a couple options you can mess with when requesting location data.

  • enableHighAccuracy — When enabled, the device will attempt to provide a more accurate location. This can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example). When disabled, the device can take the liberty to save resources by responding more quickly and/or using less power.
  • timeout — Requesting a location can take time, so you have the option to provide an upper bound in milliseconds on that wait.
  • maximumAge — This API can return cached locations. If this is set to Just 400 you may get cached locations as long as they were read in the last 400 milliseconds. If this is Nothing then the device must attempt to retrieve the current location every time.
defaultOptions : Options

The options you will want in 99% of cases. This will get you faster results, less battery drain, no surprise failures due to timeouts, and no surprising cached results.

{ enableHighAccuracy = False
, timeout = Nothing
, maximumAge = Nothing
}

The Standard PortFunnel interface

Types

type Message

Messages sent between Elm and the port JavaScript.

Opaque type, returned by now, nowWith, changes, stopChanges.

type State

Internal module state.

= LocationResponse Location
| ErrorResponse Error
| NoResponse

Responses.

LocationResponse is returned from a now or nowWith message, and for changes if you've subscriped with a changes message.

ErrorResponse is returned if there is an error.

NoResponse is sent if the processing code receives a message that is not a valid response message. Shouldn't happen.

Components of a PortFunnel.FunnelSpec

moduleName : String

The name of this module: "Geolocation".

Our module descriptor.

commander : (GenericMessage -> Cmd msg) -> Response -> Cmd msg

Responsible for sending a CmdResponse back througt the port.

Called by PortFunnel.appProcess for each response returned by process.

Always returns Cmd.none.

Initial State

The initial, empty state, so the application can initialize its state.

Sending a Message out the Cmd Port

send : (Value -> Cmd msg) -> Message -> Cmd msg

Send a Message through a Cmd port.

Conversion to Strings

toString : Message -> String

Convert a Message to a nice-looking human-readable string.

toJsonString : Message -> String

Convert a Message to the same JSON string that gets sent

over the wire to the JS code.

Simulator

makeSimulatedCmdPort : (Value -> msg) -> Value -> Cmd msg

Make a simulated Cmd port.

Non-standard Functions

isLoaded : State -> Bool

Returns true if a Startup message has been processed.

This is sent by the port code after it has initialized.

errorToString : Error -> String

Convert an Error to a string for simple printing.