GithubHelp home page GithubHelp logo

frostybee / geobee Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 1.0 25 KB

A simple, stand-alone PHP library for calculating the distance between geographical coordinates.

License: MIT License

PHP 100.00%
distance-calculation latitude-and-longitude length-converter vincenty-formula haversine-formula

geobee's Introduction

Geobee

A standalone PHP library for calculating the distance between geographical coordinates.

Installation

This library requires no dependencies.

To install it:

  • Clone this repository.
  • Or use composer: composer require frostybee/geobee

Units of Lengths

The following are the supported units of lengths along with their respective conversion factors:

  • m - Meters (default)
  • km - kilometers (1km = 1000 meters)
  • mi - Miles (1mi = 1609.344 meters)
  • nm - Nautical miles (1nm = 1852 meters)
  • yd - Yards (1yd = 0.9144)
  • ft - Feet (1ft = 0.3048 meters)

Usage

Using this library is simple and straightforward. Just instantiate the Calculator class and supply two pairs of latitude/longitude to the calculate() method as shown below.

<?php

use Frostybee\Geobee\Calculator;

/*
 * Calculate the distance from downtown Montreal to Laval.
 * From: Ville-Marie 
 *       postal code area: H3A
 *       Latitude/Longitude: 45.4987, -73.5703
 * To: Laval 
 *     postal code area: H7T 
 *     Latitude/Longitude: 45.55690, -73.7480
 */
$calculator = new Calculator();
$distance = $calculator->calculate(
    $from_latitude,
    $from_longitude,
    $to_latitude,
    $to_longitude
)->getDistance(); // Distance in meters.

Converting the Distance from Meters to other Units

Single Conversion

Use the to() method as show below. However, you must call the calculate() method once before doing any conversions.

$distance = $calculator->calculate(
    $from_latitude,
    $from_longitude,
    $to_latitude,
    $to_longitude
)->to('km'); // Get the distance in kilometers.

Or:

$distance = $calculator->to('mi', 3, false);

Multiple Conversions

  1. Use the toMany() method to convert the distance from meters to many (one or more) units of lengths:
// $calculator->toMany(array, $decimals, $round);
$results = $calculator->toMany(['km', 'mi'], 3, true);

The above call returns an associative array structured as follows:

array(2) {
  ["km"]=>
  float(15.298)
  ["mi"]=>
  float(9.506)
}
  1. Convert to all supported length units.
//$calculator->toAll($decimals, $round);
$results = $calculator->toAll(2, true);

output:

array(6) {
  ["m"]=>
  float(15297.83)
  ["km"]=>
  float(15.3)
  ["ft"]=>
  float(50189.72)
  ["yd"]=>
  float(16729.91)
  ["mi"]=>
  float(9.51)
  ["nm"]=>
  float(8.26)
}

geobee's People

Contributors

frostybee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

dheia

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.