PHP Classes

Redbox PHP Distance: Compute the distance between locations or zipcodes

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 307 All time: 7,349 This week: 49Up
Version License PHP version Categories
redbox-distance 1.2.5MIT/X Consortium ...5.4PHP 5, Web services, Geography
Description 

Author

This class can compute the distance between locations or zipcodes.

It can take the geographic coordinates of two locations and calculates the distance between them using the Google Maps API.

The class can also compute the distance between two locations given their zipcodes.

The distance may be returned in kilometers, miles or yards.

Picture of Johnny Mast
  Performance   Level  
Name: Johnny Mast <contact>
Classes: 10 packages by
Country: The Netherlands The Netherlands
Age: 42
All time rank: 121534 in The Netherlands The Netherlands
Week rank: 197 Up4 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 5x

Recommendations

Google API geocoder
Need to get lat/long and accuracy for address location

Example

<?php
require 'autoload.php';
use
Redbox\Distance;

/**
 * In this example we will calculate the distance between Amsterdam and Rotterdam (the Netherlands)
 * as Miles and display them to the user of the script.
 */
$p1 = new Distance\GeoPoint(52.364533, 4.820374); /* Amsterdam */
$p2 = new Distance\GeoPoint(51.925538, 4.471867); /* Rotterdam */

$tool = new Distance\CalculateDistance();
$distance = $tool->setSource($p1)
                 ->
setDestination($p2)
                 ->
setUseSslVerifier(false)
                 ->
getDistanceInMiles();

echo
"<h2>Distance in Kilometer</h2>";
echo
'<a href="index.php">Back to index</a><br><br />';
echo
'The calculated distance is: '.$distance.' Miles<br>';



Details

Build Status Scrutinizer Code Quality Code Coverage GitHub stars Twitter URL

Redbox-distance

This set of classes allow you to calculate the distance between Geo Locations or zipcodes. The information comes from the Google Maps API.

Features

  • Calculate the distance in * Kilometers * Miles * Yards
  • Enable/Disable SSL Verifier
  • Set Google MAPS API key (Optional, its not required at this date May 25th of 2015)

Installing via Composer

Use composer to install this package. Run the Composer command to install the latest stable version of Redbox-distance:

composer require redbox/distance

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

System Requirements

This package only requires the following:

  • Curl
  • PHP >= 5.40

Dit will also require PHPUnit 4.6 but that will be installed upon installing the package using composer.

Examples

Lets calculate the distance between Amsterdam and Rotterdam (Netherlands) using GeoLocations:

use Redbox\Distance;

$p1 = new Distance\GeoPoint(52.364533, 4.820374); /Amsterdam/
$p2 = new Distance\GeoPoint(51.925538, 4.471867); /Rotterdam/

$tool = new Distance\CalculateDistance();
$distance = $tool->setSource($p1)
                 ->setDestination($p2)
                 ->setUseSslVerifier(false)
                 ->getDistanceInKM();

echo "<h2>Example 1</h2><br>";
echo 'The calculated distance is: '.$distance.' KM<br>';

Calculate the distance between Den Helder and Haarlem (Netherlands) using posalcodes:

use Redbox\Distance;

$p1 = new Distance\GeoZipCode('1781 GC'); /Den Helder/
$p2 = new Distance\GeoZipCode("2011 SR"); /Haarlem/

$tool = new Distance\CalculateDistance();
$distance = $tool->setSource($p1)
                 ->setDestination($p2)
                 ->setUseSslVerifier(false)
                 ->getDistanceInKM();

echo "<h2>Example 2</h2><br>";
echo 'The calculated distance is: '.$distance.' KM';

Other available calculations are:

$distance->getDistanceInMiles();

and

$distance->getDistanceInYards();

Troubleshooting

If you get an error regarding SSL connectivity you might want to disable verify peer (CURLOPT_SSL_VERIFYPEER). The calculateDistance class offers a method to disable this. Consider using the setUseSslVerifier() method if needed like so.

$distance = $tool->setSource($p1)
                 ...
                 ->setUseSslVerifier(false)
                 ...

Sometimes the postalcode feature could return wrong results. To battle this also add the country to the postalcode like so.

$p1 = new GeoZipCode('1781 GC Netherlands'); /Den Helder/

Author

Redbox-distance is created and maintained by Johnny Mast. For feature requests and suggestions you could consider sending me an e-mail.

License

The MIT License

Copyright (c) 2017 Johnny Mast (mastjohnny@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


  Files folder image Files (17)  
File Role Description
Files folder imageexamples (5 files)
Files folder imagesrc (3 files)
Files folder imagetests (3 files)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data

  Files folder image Files (17)  /  examples  
File Role Description
  Accessible without login Plain text file autoload.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Example Example script
  Accessible without login Plain text file kilometer.php Example Example script
  Accessible without login Plain text file miles.php Example Example script
  Accessible without login Plain text file Yards.php Example Example script

  Files folder image Files (17)  /  src  
File Role Description
  Plain text file CalculateDistance.php Class Class source
  Plain text file GeoPoint.php Class Class source
  Plain text file GeoZipCode.php Class Class source

  Files folder image Files (17)  /  tests  
File Role Description
  Accessible without login Plain text file CalculateDistanceTest.php Test Unit test script
  Accessible without login Plain text file GeoPointTest.php Test Unit test script
  Accessible without login Plain text file GeoZipCodeTest.php Test Unit test script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:307
This week:0
All time:7,349
This week:49Up