geoposition-to-object

1.0.2 • Public • Published

geoposition-to-object

Turn an HTML5 Geoposition object into a regular object that can be serialized with JSON.stringify

Pairs well with geolocation-stream.

See also geoposition-to-geojson, a very similar module that coerces geoposition objects into a Mapbox and/or OpenStreetMap-ready format.

Why

When using the geolocation feature in browsers, the object you'll get back cannot be stringified:

navigator.geolocation.getCurrentPosition(function(position) {
  console.log(JSON.stringify(position))
})
 
// {}

This package exports a function that accepts a Geoposition and returns a plain javascript Object with all the same deep properties.

Installation

npm install geoposition-to-object --save

Usage

var objectify = require('geoposition-to-object')
 
navigator.geolocation.getCurrentPosition(function(position) {
  console.log(objectify(position))
})
 
// {
//   timestamp: '2016-07-20T21:50:03.183Z',
//   coords: {
//     accuracy: 26,
//     altitude: null,
//     altitudeAccuracy: null,
//     heading: null,
//     latitude: 38.792024999999995,
//     longitude: -104.8483681,
//     speed: null
//   }
// }

Tests

npm install
npm test

Dependencies

None

Dev Dependencies

  • tap-spec: Formatted TAP output like Mocha's spec reporter
  • tape: tap-producing test harness for node and browsers

License

MIT

Generated by package-json-to-readme

Package Sidebar

Install

npm i geoposition-to-object

Weekly Downloads

224

Version

1.0.2

License

MIT

Last publish

Collaborators

  • zeke