Coder Social home page Coder Social logo

geo-tools's Introduction

#geo-tools

A simple library used to geocode an address, reverse geocode coordinates, and calculate the distance between two locations. The default distance used is KM. Unit conversion methods are provided and can be seen below. The Google Maps API service is used for geocoding and reverse geocoding.

Installation

npm install geo-tools

Require

At the top of your app:

var geoTools = require('geo-tools');

Methods

Latitude and longitude shoud be inputted as lat/lng and will be returned as lat/lng.

geocode(address, callback, options)

Returns the latitude and longitude of a given address.

geocode('717 California Street, San Francisco, CA', function(coordinates){
  console.log(coordinates)
})

//Displays to the console:

{ lat: 37.79221, lng: -122.406141 }

Options(not required):

{key: 'YOUR_GOOGLE_API_KEY'}

reverseGeocode(object[lat], [lng], callback)

Takes 2 or 3 arguments. It can accept either an object with the lat/lng (ex. reverseGeocode({lat: 51.515400, lng: 7.455185}, callback)) or two numbers and the callback (ex. reverseGeocode(51.515400, 7.455185, callback)). If you use the latter format, order matters. Latitude must go first and longitude second.

reverseGeocode({lat: 37.79221, lng: -122.406141}, function(address){
	console.log(address)
})

//Displays to the console:

{ full_address: '717 California Street, San Francisco, CA 94108, USA',
  street_number: '717',
  street: 'California Street',
  neighborhood: 'Chinatown',
  city: 'San Francisco',
  county: 'San Francisco County',
  state: 'California',
  country: 'United States',
  postal_code: '94108' }

distance(object1, object2[lat1], [lng1], [lat2], [lng2])

Calculates the distance between two sets of coordinates. Returns the distance in Km. Accepts either 2 object arguments (ex. distance({lat: 37.79221, lng: -122.406141}, {lat: 37.774514, lng: -122.418079})), or 4 number arguments (ex. distance(37.79221, -122.406141, 37.774514, -122.418079)). If you choose the latter method, order matters and should be as follows: distance(lat1, lng1, lat2, lng2). Passing in objects is reccomended.

distance({lat: 37.79221, lng: -122.406141}, {lat: 37.774514, lng: -122.418079})

//Returns:

2.2299158844532245

toMiles(distance)

Converts Km into miles. The argument passed must be in Km which is the default unit used in geo-tools.

var length = distance({lat: 37.79221, lng: -122.406141}, {lat: 37.774514, lng: -122.418079});

toMiles(length) //Returns

1.3856050630385846

toMeters(distance)

Converts Km into meters. The argument passed must be in Km which is the default unit used in geo-tools.

toYards(distance)

Converts Km into yards. The argument passed must be in Km which is the default unit used in geo-tools.

toFeet(distance)

Converts Km into feet. The argument passed must be in Km which is the default unit used in geo-tools.

Tests

To run tests, type mocha into your terminal

mocha

Notes

  • Sexagesimal format is not supported
  • Google Maps API will allow for up to 2500 API calls in 24 hours. For additional calls, a business API key is required.

Sources

Any and all feedback/suggestions is appreciated and should be sent to: [email protected]

Enjoy!

geo-tools's People

Contributors

joeloria avatar

Watchers

James Cloos avatar Faizan Sattar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.