-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt 1 at creating a composer package, putting it on Packagist, an…
…d including it in my project.
- Loading branch information
Steven Barnett
committed
Feb 24, 2015
1 parent
1757499
commit 29835f8
Showing
5 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "stevendesu/world-distance", | ||
"description": "A Laravel 5 package for calculating the distance between two given points on a globe. Optionally uses Google's Distance Matrix API. By default just uses math and assumes a spherical Earth.", | ||
"keywords": ["laravel", "laravel 5", "distance", "matrix", "geography"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Steven Barnett", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0", | ||
"illuminate/support": "~5.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Stevendesu\\WorldDistance\\": "src" | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
return array( | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Earth radius | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This is the value that will be used in distance calculations. The unit on | ||
| this value equals the unit that will be returned. e.g. if this value is | ||
| in meters, the return value will be in meters. | ||
| | ||
*/ | ||
|
||
'earth_radius' => 6378100, // Meters | ||
|
||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php namespace Stevendesu\WorldDistance; | ||
|
||
// For Google Maps API caching | ||
use Stevendesu\WorldDistance\WorldDistanceRecord; | ||
|
||
// For logging major fail | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
|
||
// Default like units, base location, or Earth size | ||
use Illuminate\Config\Repository; | ||
use Illuminate\Database\DatabaseManager; | ||
|
||
class WorldDistance { | ||
|
||
/** | ||
* Illuminate config repository instance. | ||
* | ||
* @var \Illuminate\Config\Repository | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* Illuminate database manager instance. | ||
* | ||
* @var \Illuminate\Database\DatabaseManager | ||
*/ | ||
protected $database; | ||
|
||
/** | ||
* Create a new WorldDistance instance. | ||
* | ||
* @param \Illuminate\Config\Repository $config | ||
* @param \Illuminate\Database\DatabaseManager $database | ||
*/ | ||
public function __construct(Repository $config, DatabaseManager $database) { | ||
$this->config = $config; | ||
$this->database = $database; | ||
} | ||
|
||
// calculate the distance in kilometers | ||
public function getDistance($LatLng1, $LatLng2, $googleMaps = false) { | ||
if( $googleMaps ) { | ||
return $this->getGoogleMapsDistance($LatLng1, $LatLng2); | ||
} | ||
|
||
$dLat = deg2rad($LatLng2[0] - $LatLng1[0]); | ||
$dLon = deg2rad($LatLng2[1] - $LatLng1[1]); | ||
|
||
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($LatLng1[0])) * cos(deg2rad($LatLng2[0])) * sin($dLon/2) * sin($dLon/2); | ||
$b = 2 * asin(sqrt($a)); | ||
$c = $this->config->get('worlddistance.radius') * $b; | ||
|
||
return $c; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php namespace Stevendesu\WorldDistance; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class WorldDistanceFacade extends Facade { | ||
|
||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() { return 'distance'; } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php namespace Stevendesu\WorldDistance; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class WorldDistanceServiceProvider extends ServiceProvider { | ||
|
||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->package('stevendesu/world-distance'); | ||
|
||
$this->publishes([ | ||
__DIR__.'/../config/worlddistance.php' => config_path('worlddistance.php'), | ||
]); | ||
} | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// Register providers. | ||
$this->app['distance'] = $this->app->share(function($app) | ||
{ | ||
return new WorldDistance($app['config'], $app['db']); | ||
}); | ||
} | ||
|
||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return ['distance']; | ||
} | ||
|
||
} |