Search and places
Search covers named places, streets, local government areas and states from OpenStreetMap, together with MilsatMaps' own surveyed places, which rank first when they match as well. Matching ignores case and accents and tolerates typos.
const map = new milsat.Map(document.getElementById("map"), {
apiKey: "YOUR_API_KEY",
center: [3.3792, 6.5244],
zoom: 12,
});
const output = document.getElementById("output");
const results = await milsat.geocode("pharmacy", { near: map.getCenter(), limit: 5 });
const lines = [];
for (const result of results) {
new milsat.Marker({ position: result.location, map, title: result.name });
lines.push(`${result.name} · ${result.category} · ${result.context ?? ""} (${result.source})`);
}
output.textContent = lines.join("\n");
const first = results[0];
if (first) {
const place = await milsat.places.details(first.id);
output.textContent += "\n\nDetails of the first:\n" + JSON.stringify(place.attributes, null, 2);
}geocode(query, options)
Places matching query, best first. Pass near (usually the map's centre) to prefer closer matches, and limit (1 to 20, default 8).
| Field | |
|---|---|
id | For places.details. |
name | |
category | Such as pharmacy, city, residential. |
kind | place, region, poi, street or postcode. |
context | The area it is in, such as "Yaba, Lagos". |
location | { lng, lat } |
source | milsat for our own places, osm for OpenStreetMap. |
Full street addresses such as "12 Allen Avenue, Ikeja" are not broken into parts yet: search for the street or the place instead.
places.autocomplete(input, options)
For type-ahead boxes. Returns up to six predictions, each with a placeId and a ready-to-show description. It waits 150 ms for typing to pause before asking (set debounceMs to change that). Cancel the previous call as the user types: a call cancelled while it waits is never sent, so only the pauses count as requests.
let controller;
input.addEventListener("input", async () => {
controller?.abort();
controller = new AbortController();
try {
const predictions = await milsat.places.autocomplete(input.value, {
near: map.getCenter(),
signal: controller.signal,
});
showSuggestions(predictions.map((p) => p.description));
} catch (error) {
if (error.name !== "AbortError") throw error;
}
});places.details(id)
A place in full: its geometry, its attributes (opening hours, phone and so on, where known) and its area. The id comes from a search result, a prediction, or a featureclick event.
reverseGeocode(position)
What is at a point: a place if there is one, the nearest street, and the area. Click the map:
const map = new milsat.Map(document.getElementById("map"), {
apiKey: "YOUR_API_KEY",
center: [3.3515, 6.6018],
zoom: 15,
});
map.on("click", async (event) => {
const here = await milsat.reverseGeocode(event.lngLat);
new milsat.Popup({ text: here.label }).open(map, event.lngLat);
});