How to Show Address Suggestions Near the User First, or Only Within One City

API or service
Address Autocomplete API + @geoapify/geocoder-autocomplete
Task
Typed text + user position or city area → bias or filter → nearby or in-city suggestions
Examples
URLJSAI
Difficulty
Beginner
Time
10 min

Show address suggestions near the user first, or only within one city

A user types the beginning of an address, and the same street name and house number exist in many towns. 300 Hennepin is a downtown Minneapolis address, but it also exists in Wahkon, Monticello, and Glencoe, Minnesota. The Address Autocomplete API can rank the matches near the user first, or return only matches inside your city, depending on the request.

Use a bias when nearby addresses should come first but others can still appear. Use a filter when an address outside the area is invalid for your app, for example for a delivery service that works in one city. You can also combine both: only the city, with the addresses nearest to the user first.

Task flow: typed text + user position or city area → bias=proximity and/or filter=rect → suggestions near the user or inside the city.

Choose a bias or a filter

You want Parameter What happens to other places
Addresses near the user first bias=proximity:lon,lat Still returned, ranked lower
Only addresses in one city filter=rect:lon1,lat1,lon2,lat2 (the city's bounding box) Not returned
Only addresses within a radius, such as a delivery area filter=circle:lon,lat,radiusMeters Not returned
Only the city, nearest to the user first filter=rect:... and bias=proximity:... Not returned; the rest ranked by the user's position
  • A bias changes the order. It never removes results, so a user can still find an address in another town.
  • A filter removes results outside the area. If nothing matches inside it, the API returns an empty list, so show a "no address found in this area" message instead of an empty dropdown.
  • Write longitude before latitude in proximity, rect, and circle.
  • Without your own bias, Autocomplete ranks results using the approximate location of the requester's IP address. When the requests go through your backend, that is the server's location, not the user's, so pass a bias explicitly.

For country-level limits, see Restrict address search to a country.

Rank suggestions near the user

Add bias=proximity:lon,lat with the user's position. This request uses a point in downtown Minneapolis:

https://api.geoapify.com/v1/geocode/autocomplete?text=300%20Hennepin&bias=proximity:-93.2655,44.9773&limit=4&format=json&apiKey=YOUR_API_KEY
# Suggestion distance
1 300 Hennepin Avenue, Minneapolis, MN 55401 475 m
2 Minneapolis Central Library, 300 Hennepin Avenue, Minneapolis, MN 55401 513 m
3 Community Information Board, 300 Hennepin Avenue, Minneapolis, MN 55401 498 m
4 Minerva, 300 Hennepin Avenue, Minneapolis, MN 55401 508 m

Without the bias, the same request sent from outside Minnesota returned one Minneapolis row and three in other towns: 300 Hennepin Street North, Wahkon; 300 Hennepin Street, Monticello; and 300 Hennepin Avenue North, Glencoe. With the bias, every result is the Minneapolis address and places at it.

With a proximity bias, each result has a distance field: the distance in meters from the bias point. The order also reflects how well each result matches the text, so it is not strictly sorted by distance.

Use the browser position with @geoapify/geocoder-autocomplete

In the browser, ask for the position with the Geolocation API and pass it to addBiasByProximity(). The field works while the user decides, and keeps the default ranking if they decline:

<label>Delivery address</label>
<div id="autocomplete" style="position: relative"></div>
import { GeocoderAutocomplete } from "@geoapify/geocoder-autocomplete";
import "@geoapify/geocoder-autocomplete/styles/minimal.css";

const autocomplete = new GeocoderAutocomplete(
  document.querySelector("#autocomplete"),
  "YOUR_API_KEY",
  { placeholder: "Enter an address", limit: 4, skipIcons: true }
);

// Rank suggestions near the user once the browser shares a position
navigator.geolocation?.getCurrentPosition(
  ({ coords }) => {
    autocomplete.addBiasByProximity({ lon: coords.longitude, lat: coords.latitude });
  },
  () => {
    // Permission denied or no position: keep the default ranking
  },
  { timeout: 5000, maximumAge: 10 * 60 * 1000 }
);

The library sends the bias with every following request. Browsers ask for geolocation permission only on HTTPS pages (and localhost). If the app already knows a better point, such as the store the user picked or the center of the visible map, pass that point instead.

Return only addresses in one city

Get the city's bounding box once from the Forward Geocoding API with type=city:

https://api.geoapify.com/v1/geocode/search?text=Minneapolis%2C%20MN&type=city&limit=1&format=json&apiKey=YOUR_API_KEY
{
  "formatted": "Minneapolis, MN, United States of America",
  "result_type": "city",
  "lat": 44.9772995,
  "lon": -93.2654692,
  "bbox": {
    "lon1": -93.329125,
    "lat1": 44.8906175,
    "lon2": -93.193859,
    "lat2": 45.0512498
  }
}

Reduced response: only the fields used here. The bounding box does not change often, so store it in your configuration instead of requesting it on every page load.

Pass the box as filter=rect:lon1,lat1,lon2,lat2:

https://api.geoapify.com/v1/geocode/autocomplete?text=300%20Hennepin&filter=rect:-93.329125,44.8906175,-93.193859,45.0512498&limit=4&format=json&apiKey=YOUR_API_KEY

Every result is now inside the rectangle: 300 Hennepin Avenue, Minneapolis, MN 55401, and the three places at that address. The rows in Wahkon, Monticello, and Glencoe are no longer returned, wherever the request comes from.

A bounding box is a rectangle around the city, so it can also include edges of neighboring towns. When the exact city limit matters, for example for a delivery service, check the selected address before accepting it, for example with its city field.

In @geoapify/geocoder-autocomplete, the JSON bbox already has the shape addFilterByRect() expects:

autocomplete.addFilterByRect({ lon1: -93.329125, lat1: 44.8906175, lon2: -93.193859, lat2: 45.0512498 });

For a radius around a store or depot, use filter=circle:lon,lat,radiusMeters in the API, or addFilterByCircle({ lon, lat, radiusMeters }) in the library.

Combine the city filter with a bias toward the user

For a service that works in one city, filter to the city and bias toward the user. Until the browser shares a position, bias toward the city center, so the order does not depend on the user's IP address:

import { GeocoderAutocomplete } from "@geoapify/geocoder-autocomplete";
import "@geoapify/geocoder-autocomplete/styles/minimal.css";

// Minneapolis, MN: bbox and center from the Forward Geocoding API (type=city)
const city = {
  bbox: { lon1: -93.329125, lat1: 44.8906175, lon2: -93.193859, lat2: 45.0512498 },
  center: { lon: -93.2654692, lat: 44.9772995 }
};

const autocomplete = new GeocoderAutocomplete(
  document.querySelector("#autocomplete"),
  "YOUR_API_KEY",
  { placeholder: "Enter a Minneapolis address", limit: 4, skipIcons: true }
);

// Only addresses inside the city's rectangle
autocomplete.addFilterByRect(city.bbox);

// Rank from the city center until the browser shares the user's position
autocomplete.addBiasByProximity(city.center);

navigator.geolocation?.getCurrentPosition(
  ({ coords }) => {
    // Replaces the city-center bias
    autocomplete.addBiasByProximity({ lon: coords.longitude, lat: coords.latitude });
  },
  () => {
    // Permission denied or no position: keep the city-center bias
  },
  { timeout: 5000, maximumAge: 10 * 60 * 1000 }
);

autocomplete.on("select", (place) => {
  if (!place) return;
  console.log(place.properties.formatted, place.properties.lat, place.properties.lon);
});

The library keeps one bias of each type, so the second addBiasByProximity() call replaces the city center. Each request then carries both parameters, for example filter=rect:-93.329125,44.8906175,-93.193859,45.0512498&bias=proximity:-93.2655,44.9773.

The Geoapify address autocomplete field labeled "Delivery address" with "300 Hennepin" typed; all four suggestions are at 300 Hennepin Avenue, Minneapolis, MN 55401: the address itself, Minneapolis Central Library, Community Information Board, and Minerva

The screenshot was rendered from this code with @geoapify/geocoder-autocomplete 3.1.0 and the minimal.css theme, with the browser position set to downtown Minneapolis.

To change the area later, for example when the user switches city, call clearFilters() and clearBias(), then add the new filter and bias.

Nearby or in-city suggestions with an AI coding agent

Copy this prompt into Codex, Claude Code, Cursor, or another coding agent so the address autocomplete in your project shows addresses near the user first, or only inside the area you serve.

Update the address autocomplete in my project (Geoapify Address Autocomplete API or @geoapify/geocoder-autocomplete) so suggestions come from the right area.

Before you start
- Inspect the existing project first. Reuse its framework, autocomplete component, HTTP client, configuration, and conventions. Make the smallest necessary changes.
- Ask only what the project doesn't answer: should addresses outside the service area be excluded (filter) or only ranked lower (bias)? Which city or radius is served? Can the app ask for the browser position?
- Use the configured Geoapify API key. Never hardcode, print, or log it.

API rules
1. Bias near the user: bias=proximity:lon,lat. In the library: addBiasByProximity({ lon, lat }).
2. Only one city: get its bbox once from GET https://api.geoapify.com/v1/geocode/search?text=<city>&type=city&limit=1&format=json, store it in configuration, and send filter=rect:lon1,lat1,lon2,lat2. In the library: addFilterByRect(bbox) — the JSON bbox has the same shape.
3. A radius: filter=circle:lon,lat,radiusMeters, or addFilterByCircle({ lon, lat, radiusMeters }).
4. Combine a filter with a proximity bias to keep only the area and rank by the user. Until a browser position is available, bias toward the city center.

Requirements
- Longitude before latitude in proximity, rect, and circle.
- Without an explicit bias the API ranks by the requester's IP location; behind a backend proxy that is the server, so always send a bias.
- Request the browser position only on HTTPS, keep the field usable while permission is pending, and keep working if it is denied.
- With a filter, show a clear "no address found in this area" state for empty results.
- A bbox can include edges of neighboring towns; when the exact city matters, validate the selected address (for example its city field) before accepting it.
- To change the area, call clearFilters() and clearBias() before adding new ones.

Review the generated code against this guide. Check that it uses a filter only where outside addresses are invalid, keeps longitude before latitude, falls back to the city center when the position is unavailable, and handles an empty result list.