Giter Site home page Giter Site logo

orafaelfragoso / php-haversine-formula Goto Github PK

View Code? Open in Web Editor NEW
44.0 6.0 18.0 543 KB

Calculates de distance between two geocode points ๐Ÿ“Œ๐Ÿ—บ

License: MIT License

PHP 92.57% Makefile 7.43%
php php7 google-distance-matrix longitude latitude

php-haversine-formula's Introduction

php-haversini-formula Build Status

Calculates de distance between two geocode points ๐Ÿ“Œ๐Ÿ—บ

How to use it:

Install the library via Composer or clone the repo:

composer install rafaelfragoso/haversini-formula

After the installation is complete, you can load the class and start using it.

<?php

use Haversini\Haversini;

/*
 * Calculate distance from Rio de Janeiro to Sรฃo Paulo
 * Rio: -22.906847, -43.172896
 * Sรฃo Paulo: -23.550520, -46.633309
 */
Haversini::calculate(
    -22.906847,
    -43.172896,
    -23.550520,
    -46.633309,
    'mi' // Output length unit
);

Running tests

make test or make test-coverage

XDebug is required for test coverage!

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

php-haversine-formula's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

php-haversine-formula's Issues

set precision

This works AMAZING! Thank you for making and sharing this little diddy. For my purposes, I have re-worked the code slightly to allow users to specify the precision of rounding the results, or default to 0 if it is not specified. My project has some less than 1 mile away so I liked the precision to have 3 decimal places. If you like, I encourage you to update the Distance class

class Distance {

	private static function calculate($fromLat, $fromLon, $toLat, $toLon, $precision = 0) {
		$earth_radius = 6371;

		$dLat = deg2rad($toLat - $fromLat);
		$dLon = deg2rad($toLon - $fromLon);
		
		$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($fromLat)) * cos(deg2rad($toLat)) * sin($dLon/2) * sin($dLon/2);
		$c = 2 * asin(sqrt($a));
		$d = $earth_radius * $c;
		
		return round($d, $precision);
	}
  
  public static function toKilometers($fromLat, $fromLon, $toLat, $toLon, $precision = 0) {
	  return self::calculate($fromLat, $fromLon, $toLat, $toLon);
	}

  public static function toMiles($fromLat, $fromLon, $toLat, $toLon, $precision = 0) {
    $distance = self::calculate($fromLat, $fromLon, $toLat, $toLon, $precision);
	  return round($distance * 0.621371192, $precision);
	}

}

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.