Static map previews in Laravel: same pin, a fraction of the cost
November 19, 2026
We built an address management package for Filament. It has a map preview: a small map that updates as you geocode an address, so the user can confirm the pin landed in the right place.
Simple feature. Expensive by default.
The problem
The default choice for mapping is the Google Maps JavaScript API: add the script tag, render a map, drop a marker. It works, and every map load is billed under the Dynamic Maps SKU at $7 per 1,000 loads, after a free allowance of 10,000 loads a month per billing account.
Think about where this component lives. An address resource in an admin panel loads the map on every create, view and edit, for every record a staff member opens. Take 5,000 addresses viewed an average of 3 times a month, which isn't unusual for a mid-size operation. That's 15,000 map loads:
(15,000 − 10,000 free) × $0.007 = $35/month. For a preview map nobody pans or zooms.
So we asked: does this map need to be interactive at all?
For an address preview, no. The user needs to see where the pin landed, zoomed in close enough to read street names. A static image does that. We add a small overlay button that opens the location in Google Maps in a new tab for anyone who wants to explore.
The Static Maps API
Google's Maps Static API returns a plain image for a given centre, zoom and size. It has the same 10,000 free requests a month, then costs $2 per 1,000, 3.5 times less than a dynamic map load.
Same scenario, 15,000 views a month:
(15,000 − 10,000 free) × $0.002 = $10/month.
| Dynamic Maps (JS) | Static Maps + proxy | |
|---|---|---|
| Free each month | 10,000 loads | 10,000 requests |
| Then | $7 / 1,000 | $2 / 1,000 |
| 5k addresses × 3 views | $35/month | $10/month |
| 20k addresses × 3 views | $350/month | $100/month |
Every view is still a billed request. The saving is the per-request price, and it grows with traffic.
The proxy
You can't call the Static Maps API straight from the browser without putting your API key in the image URL. You can restrict a browser key by referrer, but we'd rather keep the server key off the page entirely. So the image request goes through a Laravel controller that holds the key.
// routes/web.php
Route::get('/map-image', MapImageController::class)
->middleware('throttle:60,1');
<!-- In the Blade/Alpine component -->
<img :src="`/map-image?lat=${lat}&lng=${lng}`" />
The controller validates the coordinates, asks Google for the image, and passes it back:
public function __invoke(Request $request): Response
{
// 4 decimal places ≈ 11 m, plenty for a preview
$lat = round((float) $request->input('lat'), 4);
$lng = round((float) $request->input('lng'), 4);
if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180) {
return response('Invalid coordinates', 400);
}
$query = http_build_query([
'center' => "{$lat},{$lng}",
'zoom' => 15,
'size' => '640x400',
'scale' => 2,
'format' => 'jpg',
'key' => config('services.google_maps.server_key'),
]);
// Appended by hand so the | delimiter goes out as %7C exactly as written
$url = "https://maps.googleapis.com/maps/api/staticmap?{$query}"
. "&markers=color:red%7C{$lat},{$lng}";
$google = Http::timeout(10)->get($url);
if (! $google->successful()) {
return response('Failed to fetch map image', 502);
}
$response = new Response($google->body(), 200, ['Content-Type' => 'image/jpeg']);
$response->setPrivate();
// Let the browser reuse the image for as long as Google says it may
if (preg_match('/max-age=(\d+)/', $google->header('Cache-Control'), $m)) {
$response->setMaxAge((int) $m[1]);
}
return $response;
}
A few details worth noting:
No server-side storage. The image goes from Google to the browser and the server keeps nothing. More on why below.
Browser caching follows Google. Google sends its own Cache-Control: max-age with each image (24 hours at the time of writing). We pass that through, so reopening the same record in the same browser that day doesn't trigger a new request. setPrivate() stops a CDN or shared proxy in front of your app from storing the image for other users.
Rate limiting matters more now. Every request that reaches the controller is a billed call to Google, so the throttle middleware is what stands between a scripted loop and your invoice.
Input validation. The package version also checks the size format (\d+x\d+), the zoom range (0–21) and a maximum image size before calling the API.
Why we don't cache the image
Once every image passes through your server, the obvious next step is to store it and serve repeat views from disk. On paper that turns 15,000 billed requests into 5,000, one per address.
Google's terms don't allow it. The Google Maps Platform Terms of Service, section 3.2.3(b):
No Caching. Customer will not cache Google Maps Content except as expressly permitted under the Maps Service Specific Terms.
The Service Specific Terms allow caching for a few things, such as place IDs and some geocoding and Address Validation results, each with its own limits. They allow nothing for Maps Static API images, for any duration. The "30 days" figure you sometimes see belongs to geocoding coordinates, not map images.
So the proxy keeps nothing, and the rest of the package doesn't cache Google responses either. If you run a proxy like this on your own key, it's worth reading those two documents yourself. This is our reading of them, not legal advice.
Visibility: audit logging
Every map view passes through your server, so you can count them at almost no cost:
DB::table('address_api_audit_logs')->insert([
'operation_type' => 'static_map',
'provider' => 'google',
'was_successful' => $google->successful(),
'response_time_ms' => $responseTimeMs,
'created_at' => now(),
]);
Without caching, that count is your Static Maps bill. You can see how close you are to the 10,000 free requests before the invoice arrives, and which screens drive the usage. Log only metadata like this: storing Google's response would be caching it.
What you give up
The interactive map. For an address preview, that's the right trade. If your users need to pan, zoom or click to place a pin, Static Maps isn't a substitute and the JavaScript API is the right tool.
The result
One route, one small controller, an <img> tag. The API key stays on the server and nothing from Google is stored.
$35/month → $10/month in the example above, and the gap widens with traffic: at 20,000 addresses it's $350 versus $100. Under 10,000 views a month, both are free.
This proxy is included in Filament Address Pro, a package for international address management in Filament. The approach works anywhere you'd otherwise load the Maps JavaScript API for a non-interactive preview, no package required.