## anthonymartin/geo-location

Powerful GeoCoding library: Retrieve bounding box coordinates, distances between geopoints, point in polygon, get longitude and latitude from addresses and more with GeoLocation for PHP

##### Details

Homepage

Source

Issues

249 480

0

153

14

59

Type:class

v2.0.1 2019-09-18 19:00 UTC

This package is auto-updated.

Last update: 2020-09-18 21:21:55 UTC

GeoLocation for PHP offers convenient and easy to use methods for geocoding, geolocation and geometry functions in PHP.

Features include:

1. Retrieve bounding box coordinates. Just define a coordinate and the size of your bounding box.
2. calculate distances between geopoints/coordinates
3. Solve point in polygon problems (identify whether a given point is within the bounds of a polygon)

# Examples

## Get distance between two points:

```<?php

use AnthonyMartin\GeoLocation\GeoPoint;

\$geopointA = new GeoPoint(40.5187154, -74.4120953);
\$geopointB = new GeoPoint(40.65, -73.95);
\$geopointB = \$geopointA->distanceTo(\$geopointB, 'miles');```

## Get latitude and longitude from address or location

In order to use this method, you'll need to register at Google Cloud Console and enable the Geocoding API

```<?php
use AnthonyMartin\GeoLocation\GeoPoint;

\$latitude = \$geopoint->getLatitude();
\$longitude = \$geopoint->getLongitude();```

## Get bounding box coordinates

```<?php

use AnthonyMartin\GeoLocation\GeoPoint;

\$geopointA = new GeoPoint(40.5187154, -74.4120953);
\$boundingBox = \$geopointA->boundingBox(3, 'miles');
\$boundingBox->getMaxLatitude();
\$boundingBox->getMaxLongitude();
\$boundingBox->getMinLatitude();
\$boundingBox->getMinLongitude();```

## How to find if coordinates/geopoint are in a polygon.

```<?php

use AnthonyMartin\GeoLocation\GeoPoint;
use AnthonyMartin\GeoLocation\Polygon;

\$geopointA = new GeoPoint(40.5187154, -74.4120953);
\$polygon = Polygon::fromArray(array(
[\$lat1, \$lon1],
[\$lat2, \$lon2],
[\$lat3, \$lon3],
[\$lat4, \$lon4]
));
if (\$geopointA->inPolygon(\$polygon)) {
echo "GeoPoint is in Polygon!";
}```

## Transform bounding box coordinates into a polygon

```<?php

use AnthonyMartin\GeoLocation\GeoPoint;

\$geopointA = new GeoPoint(40.5187154, -74.4120953);
\$boundingBox = \$geopointA->boundingBox(5, 'mi');
\$polygon = \$boundingBox->toPolygon();```

and now you can check if the GeoPoint is the polygon / bounding box:

```if (\$geopointA->inPolygon(\$polygon)) {
echo "GeoPoint is in Polygon / Bounding Box!";
}```

## Running Tests

Run the following from the project directory:

`./vendor/bin/phpunit tests`

## Credits and Legal

This is a collection of PHP classes written by Anthony Martin. Some of GeoLocation.php was derived from Java code that was originally published at http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates.

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, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.