Geocoding: Show place search results
This tutorial shows how you can use place search using the MapTiler Geocoding API. The resultant map application takes a search query from the URL and displays the search results on the map, zooming in on the first result.
The map shows as a red dot the first search result for “Austin”.
NPM module setup
-
Install the npm package.
npm install --save @maptiler/sdk -
Include the CSS file.
If you have a bundler that can handle CSS, you can
importthe CSSimport "@maptiler/sdk/dist/maptiler-sdk.css";or include it with a
<link />in the head of the document via the CDN<link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.css' rel='stylesheet' /> -
Include the following code in your JavaScript file (Example: app.js).
import * as maptilersdk from '@maptiler/sdk'; maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE'; const map = new maptilersdk.Map({ container: 'map', // container's id or the HTML element to render the map style: maptilersdk.MapStyle.STREETS, center: [-97.7485, 30.2711], // starting position [lng, lat] zoom: 11.7, // starting zoom }); -
Replace
YOUR_MAPTILER_API_KEY_HEREwith your own API key. Make sure to secure the key before you publish it. -
The next is up to you. You can center your map wherever you desire (modifying the
starting position) and set an appropriate zoom level (modifying thestarting zoom) to match your users’ needs. Additionally, you can change the map’s look (by updating thesource URL); choose from a range of visually appealing map styles from our extensive MapTiler standard maps, or create your own to truly differentiate your application. -
Get the query string of the map URL.
const urlParams = new URLSearchParams(window.location.search); -
Create the map layer where we will display the search results.
map.on('load', async () => { map.addSource('search-results', { type: 'geojson', data: { "type": "FeatureCollection", "features": [] } }); map.addLayer({ 'id': 'point-result', 'type': 'circle', 'source': 'search-results', 'paint': { 'circle-radius': 8, 'circle-color': '#B42222', 'circle-opacity': 0.5 }, 'filter': ['==', '$type', 'Point'] }); }); -
Check if the search parameter called
qexists. If it exists, call the forward geocoding function with the value of the parameterq.if (urlParams.get('q')) { const results = await maptilersdk.geocoding.forward(urlParams.get('q')); } -
Show the results on the map and zoom in on the first result.
if (urlParams.get('q')) { const results = await searchPlace(urlParams.get('q'), key); map.getSource('search-results').setData(results); if (results.features[0]) { map.fitBounds(results.features[0].bbox, {maxZoom: 19}) } }
Learn more
Check out the tutorials on How to search places using the geocoder component and How to search places by coordinates (reverse geocoding).
Visit the MapTiler Geocoding API reference for all search options. For example specifying the language of the results, etc.
If you want to learn how to display the search results in a list, check out the Reverse Geocoding tutorial (point 6 onwards).
Related examples
Geocoding limit results by area
ExamplesLimit geocoding search results to a specific bounding box area.