Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rapidwebltd/php-simple-google-maps-api
🗺 This library allows a user to easily communicate with the Google Maps API and retrieve a set of coordinates from an address.
https://github.com/rapidwebltd/php-simple-google-maps-api
distance google-api google-maps latlong php-library
Last synced: about 1 month ago
JSON representation
🗺 This library allows a user to easily communicate with the Google Maps API and retrieve a set of coordinates from an address.
- Host: GitHub
- URL: https://github.com/rapidwebltd/php-simple-google-maps-api
- Owner: rapidwebltd
- License: lgpl-3.0
- Created: 2017-03-23T09:07:40.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-29T22:11:17.000Z (almost 7 years ago)
- Last Synced: 2024-02-12T15:08:19.694Z (10 months ago)
- Topics: distance, google-api, google-maps, latlong, php-library
- Language: PHP
- Homepage:
- Size: 18.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
This library allows a user to easily communicate with the Google Maps API and retrieve a set of coordinates from an address.
## Installation
To install, just run the following composer command.`composer require rapidwebltd/simple-google-maps-api`
Remember to include the `vendor/autoload.php` file if your framework does not do this for you.
## Usage
Creating the SimpleGoogleMaps Object can be done in one of two ways.
The first way is to pass through just the API key that you are provided upon registering for the Google Maps API.
```php
use RapidWeb\SimpleGoogleMaps\Factories\SimpleGoogleMapsFactory;
$simpleGoogleMaps = SimpleGoogleMapsFactory::getByKey("[APIKEY]");
```The second way is to provide your client name and the crypt key that you are provided with upon creating a Google enterprise account.
```php
use RapidWeb\SimpleGoogleMaps\Factories\SimpleGoogleMapsFactory;
$simpleGoogleMaps = SimpleGoogleMapsFactory::getByClientNameAndCryptKey("[CLIENTNAME]","[CRYPTKEY]");
```Once you have created the object you can then get the coordinates from an address via the `getByAddress` method.
```php
$addressline = "10 Downing St, Westminster, London SW1A UK";
$homeCoords = $simpleGoogleMaps->getByAddress($addressline);
```Optionally, you can allow partial matches to be returned if your input address is not highly accurate. You can do so with the `allowPartialMatches` method, as shown below.
```php
$simpleGoogleMaps->allowPartialMatches();
$homeCoords = $simpleGoogleMaps->getByAddress('test address');
```The above method will return a object of type LatLong, this allows you to access the coordinates like so.
```php
$latitude = $homeCoords->lat;
$longitude = $homeCoords->long;
```You can also calculate the distance between two LatLong objects by using the `distanceTo` method on the LatLong Object.
```php
$milesBetween = $homeCoords->distanceTo($toCoords);
```By default this will return the answer in miles, you may also request the distance in kilometres by adding it to the method call.
```php
$milesBetween = $homeCoords->distanceTo($toCoords,"kilometres");
```