Your user is looking at a map of downtown Seattle. They type "400 Broad St" into the search box next to it. Which 400 Broad Street do they mean? Almost certainly the one on the map: the Space Needle's address. But the search box doesn't know about the map, so it offers Des Moines, Utica and Georgetown too.
In this tutorial we connect the two. The search box gets a bias to the map center, so nearby addresses come first, and an "Only in this map area" switch that turns on a filter, so only addresses on the visible map are suggested. Both follow the map as the user pans and zooms. At the end, we ask an AI coding assistant to build the same page.
TL;DR
-
Bias (
bias=proximity:lon,lat) changes the order: nearby results first, others can still appear. Filter (filter=rect:lon1,lat1,lon2,lat2) removes everything outside the area. - For "400 Broad St" on a Seattle map: no bias gives five different cities; the bias gives four Seattle rows and Bremerton; the map-area filter gives five Seattle rows, including the Space Needle.
- With @geoapify/geocoder-autocomplete, it's two calls on MapLibre's
moveend:addBiasByProximityandaddFilterByRect. The whole demo is 49 lines of JavaScript. - An AI assistant got the parameters right from a vague prompt, but wrote 109 lines with its own dropdown on an older MapLibre. A precise prompt got our demo in 53.
👉 Live demo: type "400 Broad St", then turn on "Only in this map area" and pan the map.
Table of Contents
- Same text, three answers
- Bias or filter: which one when?
- Step 1: A map and a search field
- Step 2: Follow the map with bias and filter
- Step 3: Show the selected address
- Let an AI assistant write it
- Summary
- FAQ
Same text, three answers
We sent "400 Broad St" to the Address Autocomplete API three times, with the map showing downtown Seattle:
| Request | Top 5 suggestions |
|---|---|
text=400 Broad St |
Seattle WA, Des Moines IA, Nevada City CA, Utica NY, Georgetown SC |
+ bias=proximity:-122.3321,47.6062 |
400 Broad Street, The Loupe Lounge, Cycletrack, Protected Bike Lane (all Seattle), 400 Broad Street West, Bremerton WA |
+ filter=rect:<visible map> |
400 Broad Street, Space Needle, The Loupe Lounge, Cycletrack, Protected Bike Lane (all Seattle) |
The bias pulled Seattle to the top but still let Bremerton in, across Puget Sound and off the map. The filter kept only what's on the map, and that made room for the Space Needle itself.
Without a bias, the result list depends on the caller. Geoapify Autocomplete uses the caller's IP location as a default bias, so a user elsewhere may see a different first row. Setting the bias from the map makes the answer depend on what the user is looking at instead.
Bias or filter: which one when?
| Bias | Filter | |
|---|---|---|
| Parameter | bias=proximity:lon,lat |
filter=rect:lon1,lat1,lon2,lat2 |
| Effect | reorders: nearby first | removes results outside the area |
| Results from elsewhere | can still appear | never appear |
| Use it for | "probably near here": a store finder, a general map search | "must be here": a delivery zone, a city map, a service area |
The demo uses both: the bias always (so the order makes sense), the filter on demand.
Step 1: A map and a search field
A full-screen MapLibre GL map with Geoapify tiles, and the autocomplete in a floating panel:
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@5.24.1/dist/maplibre-gl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@geoapify/geocoder-autocomplete@3.1.0/styles/round-borders.css">
<script src="https://unpkg.com/maplibre-gl@5.24.1/dist/maplibre-gl.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@geoapify/geocoder-autocomplete@3.1.0/dist/index.min.js"></script>
<div id="map"></div>
<div class="panel">
<div id="autocomplete" class="autocomplete-container"></div>
<label><input type="checkbox" id="area-only"> Only in this map area</label>
</div>
const map = new maplibregl.Map({
container: "map",
style: "https://maps.geoapify.com/v1/styles/osm-bright/style.json?apiKey=YOUR_API_KEY",
center: [-122.3321, 47.6062], // downtown Seattle
zoom: 13
});
const addressField = new autocomplete.GeocoderAutocomplete(
document.getElementById("autocomplete"),
"YOUR_API_KEY",
{ limit: 5, skipIcons: true, skipSelectionOnArrowKey: true }
);
The library creates its own input inside #autocomplete and calls the API as the user types. Give the container position: relative, so the suggestion list opens under the input. skipSelectionOnArrowKey: true makes ↑/↓ only highlight a suggestion; Enter or a click selects it.
We used MapLibre 5.24.1: version 6 doesn't ship the dist/maplibre-gl.js browser build that a plain <script> tag needs.
Step 2: Follow the map with bias and filter
One function reads the map and updates the search settings. It runs when the map loads, after every pan or zoom (moveend), and when the switch changes:
const areaOnlySwitch = document.getElementById("area-only");
function updateSearchArea() {
// Bias: the map center
const center = map.getCenter();
addressField.clearBias();
addressField.addBiasByProximity({ lon: center.lng, lat: center.lat });
// Filter: the visible map, only when the switch is on
addressField.clearFilters();
if (areaOnlySwitch.checked) {
const bounds = map.getBounds();
addressField.addFilterByRect({
lon1: bounds.getWest(),
lat1: bounds.getSouth(),
lon2: bounds.getEast(),
lat2: bounds.getNorth()
});
}
}
map.on("load", updateSearchArea);
map.on("moveend", updateSearchArea);
areaOnlySwitch.addEventListener("change", updateSearchArea);
-
addBiasByProximityandaddFilterByRectreplace the previous value of the same kind, so each pan just moves the point and the rectangle. -
clearFilters()is what removes the rectangle when the switch goes off. -
moveendfires once when a pan or zoom ends, not on every animation frame. - The library sends the settings with the next request:
…&filter=rect:-122.387,47.583,-122.277,47.629&bias=proximity:-122.3321,47.6062.
With the switch on, "Broadway" returns only Broadway East on Capitol Hill, the one on the map.
Step 3: Show the selected address
let marker;
addressField.on("select", (place) => {
if (marker) marker.remove();
if (!place) return; // the field was cleared
const [lon, lat] = place.geometry.coordinates; // GeoJSON order: [lon, lat]
marker = new maplibregl.Marker({ color: "#2563eb" }).setLngLat([lon, lat]).addTo(map);
map.flyTo({ center: [lon, lat], zoom: Math.max(map.getZoom(), 16) });
});
After flyTo, the map fires moveend, so the bias and the filter follow the new view. With the switch on, the next search covers that smaller area. Zoom out to search wider.
Let an AI assistant write it
We gave the task to an AI coding assistant (Claude) twice, each time in a fresh session without web access, and ran both pages with a real API key.
Prompt 1, vague:
Add Geoapify address autocomplete to a MapLibre map so that addresses near what the user is looking at come first, with an option to only show results in the visible map area. Give me one HTML file I can open in the browser.
The API part was right: bias=proximity from the map center and filter=rect from the bounds, both read at the moment of each request. It debounced typing (250 ms), cancelled outdated requests and re-searched after a pan. A careful job.
But it didn't use the autocomplete library. It wrote its own dropdown with keyboard handling: 109 lines of JavaScript, against 49 in our demo. That's code you now maintain. And it loaded MapLibre 4.7.1, an older major version, because the prompt didn't name one.
Prompt 2, precise: we named the versions, the library and its global, the two methods with their argument shapes, the events to listen to, and the marker behaviour.
The same page as our demo, in 53 lines: area mode returned only Seattle rows, and ↓ ↓ Enter picked the Space Needle and placed the marker. One layout detail showed up in testing, in its page and in ours: the open suggestion list covers the switch, so close the list (Esc) before toggling.
What made the difference:
- Name the library. Without it, the assistant builds the dropdown itself. That works, but it's twice the code.
- Name the versions. Otherwise you get the version the assistant remembers.
-
Say when to update. "On
load,moveendand switchchange" is one line, and it decides whether the search follows the map. -
Both parameters were right in both runs. The assistants knew
bias=proximityandfilter=rect; the prompt mattered for everything around them.
Building an AI agent instead of a page? The Geoapify MCP server gives agents geocoding tools directly, so an agent can look up "400 Broad St near Seattle" without writing the request itself.
Summary
- Bias reorders, filter removes. Use the bias when nearby is likely, the filter when outside is wrong.
- Set the bias from the map center and the filter from the map bounds, and update both on
moveend. - With @geoapify/geocoder-autocomplete that's
addBiasByProximityandaddFilterByRect; clear before setting, so turning the switch off really removes the filter. - A filter can return an empty list when nothing matches inside the area. Tell the user to zoom out or turn the switch off.
- AI assistants know the parameters. Your prompt decides the library, the versions and the events.
FAQ
What's the difference between bias and filter in address autocomplete?
A bias (bias=proximity:lon,lat) only changes the order: results near the point come first, others can still appear. A filter (filter=rect:…, circle:…, countrycode:…) removes results outside the area.
Why bias to the map center and not to the user's location?
Because the user is searching in the place they're looking at. Someone in Chicago planning a Seattle trip pans the map to Seattle. Use the device location when the search is about where the user is right now.
Can I combine bias and filter?
Yes, the demo does: the filter decides which results are allowed, the bias orders them by distance to the map center.
Why is the list empty with the switch on?
Nothing matching the text is inside the visible map. Zoom out, pan, or turn the switch off.
Can I filter by a circle instead of the visible rectangle?
Yes: addFilterByCircle({ lon, lat, radiusMeters }), sent as filter=circle:lon,lat,radius. A rectangle matches what the user sees on a map; a circle fits "within 5 km of the store".
Does it work with Leaflet?
Yes. Read map.getCenter() and map.getBounds() the same way (Leaflet's bounds use getWest(), getSouth(), getEast(), getNorth() too) and listen to Leaflet's moveend event.
Why does MapLibre 6 not work with the script tag?
Version 6 doesn't ship dist/maplibre-gl.js, the browser build a plain <script> tag loads. Use 5.24.1 from a CDN, or install MapLibre 6 with npm and a bundler.
Try it
- 👉 Live demo on CodePen
- 📦 Source code on GitHub: three files, no build step
- 📖 Address Autocomplete API documentation
More address tutorials in this series:
- The ZIP Already Knows the City: Autofill City and State, Then Check the Address
- Address Autocomplete in Vue 3 Without a Wrapper Package (and Without Leaks)
Get a free API key at myprojects.geoapify.com and try it on a map of your own city. Does your app's search box know where the map is? Tell us in the comments.




Top comments (0)