arcasolutions/google-map
Laravel package providing Google Maps integration for your app, with helpers to generate map views and include the required scripts. Useful for quickly embedding maps and configuring markers or map options within Laravel projects.
Warning, if you want to use the Ivory Geocoder, you need to use the Ivory Provider otherwise there is no benefit.
The library allows you to request the google map geocoding API by two different ways. You have the choice between a simple address (IP or street) or an advanced geocoder request.
If you want to use it with a business account, you can read this documentation.
use Ivory\GoogleMap\Services\Geocoding\Geocoder;
use Ivory\GoogleMap\Services\Geocoding\GeocoderProvider;
use Geocoder\HttpAdapter\CurlHttpAdapter;
$geocoder = new Geocoder();
$geocoder->registerProviders(array(
new GeocoderProvider(new CurlHttpAdapter()),
));
// Geocode an address
$response = $geocoder->geocode('1600 Amphitheatre Parkway, Mountain View, CA');
If you want to build an advanced request, read this specific documenation.
When you have requested your position, the returned object is an Ivory\GoogleMap\Services\Geocoding\GeocoderResponse.
It wraps a geocoder status & geocoder results.
The available status are defined by the Ivory\GoogleMap\Services\Geocoding\GeocoderStatus constants.
// Get the geocoder status
$status = $response->getStatus();
A request can return many results. The geocoder response wraps an array of Ivory\GoogleMap\Services\Geocoding\GeocoderResult.
// Get the geocoder results
$results = $response->getResults();
Each result wraps an human readable adress, some address & geometry informations, a partial match flag & some result types.
The method getFormattedAddress is a string containing the human-readable address of this location. Often this
address is equivalent to the "postal address," which sometimes differs from country to country. (Note that some
countries, such as the United Kingdom, do not allow distribution of true postal addresses due to licensing
restrictions).
// Get the geocoder results
$results = $reponse->getResults();
foreach ($results as $result) {
// Get the formatted address
$address = $result->getFormattedAddress();
}
The method getAddressComponents returns an array containing the separate address components. Each address_component
typically contains:
// Get the geocoder results
$results = $response->getResults();
foreach ($results as $result) {
foreach ($result->getAddressComponents() as $addressComponent) {
$longName = $addressComponent->getLongName();
$shortName = $addressComponent->getShortName();
$types = $addressComponent->getTypes();
}
}
You can filter the address components by type:
// Get the geocoder results
$results = $response->getResults();
foreach ($results as $result) {
foreach ($result->getAddressComponents('route') as $addressComponent) {
// ...
}
}
#### Geometry informations
Geometry contains the following information:
- location which is an ``Ivory\GoogleMap\Base\Coordinate``.
- location type stores additional data about the specified location. The available possibilites are describes by the
``Ivory\GoogleMap\Services\Geocoding\GeocoderLocationType`` constants.
- viewport which contains the recommended viewport for displaying the returned result, specified as ``Ivory\GoogleMap\Base\Bound``.
- bounds (optionally returned) which stores the bounding box which can fully contain the returned result, specified as
``Ivory\GoogleMap\Base\Bound``. Note that these bounds may not match the recommended viewport.
``` php
// Get the geocoder results
$results = $response->getResults();
foreach ($results as $result) {
$location = $result->getGeometry()->getLocation()
$locationType = $result->getGeometry()->getLocationType();
$viewport = $result->getGeometry()->getViewport();
$bound = $result->getGeometry()->getBound();
}
The partial match flag indicates that the geocoder did not return an exact match for the original request, though it did match part of the requested address. You may wish to examine the original request for misspellings and/or an incomplete address. Partial matches most often occur for street addresses that do not exist within the locality you pass in the request.
// Get the geocoder results
$results = $response->getResults();
foreach ($results as $result) {
$partialMatch = $result->isPartialMatch();
}
The result types is an array indicates the type of the returned result. This array contains a set of one or more tags identifying the type of feature returned in the result. For example, a geocode of "Chicago" returns "locality" which indicates that "Chicago" is a city, and also returns "political" which indicates it is a political entity.
// Get the geocoder results
$results = $response->getResults();
foreach ($results as $result) {
$types = $result->getTypes();
}
use Ivory\GoogleMap\Map;
use Ivory\GoogleMap\Overlays\Marker;
use Ivory\GoogleMap\Services\Geocoding\Geocoder;
$geocoder = new Geocoder();
// Geocode a location
$response = $this->geocode('1600 Amphitheatre Parkway, Mountain View, CA');
// Create your map
$map = new Map();
foreach ($response->getResults() as $result) {
// Create a marker
$marker = new Marker();
// Position the marker
$marker->setPosition($result->getGeometry()->getLocation());
// Add the marker to the map
$map->addMarker($marker);
}
How can I help you explore Laravel packages today?