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.
Installation Add the package via Composer:
composer require arcasolutions/google-map
Requires PHP 5.6 (note: Laravel 5.6+ is compatible but may need polyfills for older PHP features).
Basic Usage Initialize the client with your API key (from Google Cloud Console):
use ArcaSolutions\GoogleMap\GoogleMap;
$client = new GoogleMap('YOUR_API_KEY');
First Use Case: Embedding a Map Generate a simple map with markers:
$map = $client->map()
->center(40.7128, -74.0060) // New York coordinates
->zoom(12)
->addMarker('Times Square', 40.7580, -73.9855);
echo $map->getHtml();
Where to Look First
src/GoogleMap.php for methods.tests/ or examples/ directories (if present).config/services.php or bind it in a service provider:
$this->app->singleton('google-map', function ($app) {
return new GoogleMap(config('services.google-map.key'));
});
Use dependency injection to fetch the client:
public function showMap(GoogleMap $googleMap) {
$map = $googleMap->map()
->center(request('lat'), request('lng'))
->zoom(14)
->addMarker('User Location', request('lat'), request('lng'));
return view('maps.show', ['map' => $map->getHtml()]);
}
Create a Blade component for consistency:
// resources/views/components/google-map.blade.php
<div class="map-container">
{!! $map !!}
</div>
Usage:
@component('components.google-map', ['map' => $map->getHtml()])
@endcomponent
Convert addresses to coordinates:
$geocode = $client->geocode('1600 Amphitheatre Parkway, Mountain View, CA');
if ($geocode->success()) {
$lat = $geocode->getLat();
$lng = $geocode->getLng();
}
Calculate routes between points:
$directions = $client->directions()
->origin('New York, NY')
->destination('Los Angeles, CA')
->get();
if ($directions->success()) {
$polyline = $directions->getPolyline();
// Render polyline on map
}
Cache API responses to reduce calls (e.g., geocoding):
$cacheKey = 'geocode_' . md5($address);
$geocode = Cache::remember($cacheKey, now()->addHours(1), function () use ($client, $address) {
return $client->geocode($address);
});
Bind the client with configuration:
$this->app->bind('google-map', function ($app) {
return new GoogleMap(config('services.google-map.key'));
});
Store the API key in .env:
GOOGLE_MAP_API_KEY=your_key_here
Load it in config/services.php:
'google-map' => [
'key' => env('GOOGLE_MAP_API_KEY'),
],
Implement retry logic for quota limits (e.g., using spatie/laravel-queueable):
try {
$response = $client->geocode($address);
} catch (GoogleMapException $e) {
if ($e->getCode() === 403) { // Quota exceeded
return back()->withError('Retry later.');
}
}
Create a decorator for custom logic:
class CustomGoogleMap extends GoogleMap {
public function addCustomMarker($title, $lat, $lng, $icon = null) {
$marker = $this->addMarker($title, $lat, $lng);
if ($icon) {
$marker->setIcon($icon);
}
return $marker;
}
}
php-compat or upgrade the package (if possible).catch (GoogleMapException $e) {
throw new \Exception("Google Maps API Error: {$e->getMessage()}", $e->getCode());
}
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY"></script>
Add query parameters to Google Maps URLs for debugging:
$map->setLibrary('places,geometry'); // Enable additional libraries
$map->setDebug(true); // If supported (check package docs)
Extend the client to log responses:
$client->setLogger(function ($message) {
\Log::debug('Google Maps API', ['message' => $message]);
});
Ensure your API key is restricted to your domain in the Google Cloud Console.
If using HTTPS, ensure the Google Maps JS library is loaded over HTTPS:
<script src="https://maps.googleapis.com/maps/api/js"></script>
Override the default map styling:
$map->setOptions([
'styles' => [
{ "featureType": "poi", "stylers": [ { "visibility": "off" } ] }
]
]);
Extend the Marker class to support custom icons or events:
$marker = $map->addMarker('Custom', $lat, $lng)
->setIcon('custom-icon.png')
->onClick('alert("Clicked!");');
If you need to use the JavaScript SDK alongside this package, initialize it separately:
<script>
function initMap() {
// Initialize JS SDK here
}
</script>
Use the places library to search nearby locations:
$places = $client->places()
->nearby('restaurant', 40.7128, -74.0060)
->radius(1000)
->get();
Use geocoding for full-text search with latitude/longitude:
$location = $client->geocode($address);
$model->update([
'lat' => $location->getLat(),
'lng' => $location->getLng(),
]);
How can I help you explore Laravel packages today?