How to search places by coordinates (reverse geocoding)

In this step-by-step tutorial, we will demonstrate the process of finding locations using coordinates through the utilization of the MapTiler Geocoding API. The option to search by coordinates allows for the conversion of specific coordinates into corresponding place names, a technique commonly referred to as reverse geocoding.

MapTiler’s all-in-one Geocoding search API offers users the ability to effortlessly locate specific places either by their names or addresses (direct geocoding) or by inputting coordinates (reverse geocoding).

Click on the map to search for places.

NPM module setup

  1. Install the npm package.

    
     npm install --save @maptiler/sdk
    
  2. Include the CSS file.

    If you have a bundler that can handle CSS, you can import the CSS

    
     import "@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' />
    
  3. 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: [13.3975, 52.5196], // starting position [lng, lat]
       zoom: 11, // starting zoom
          
     });
    
  4. Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

  5. 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 the starting zoom) to match your users’ needs. Additionally, you can change the map’s look (by updating the source 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.

  6. Create the map layer where we will display the search results.

    
     map.on('load', () => {
       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']
       });
     });
    
  7. Create the function to perform the reverse geocoding search when the map is clicked. Show the results on the map and adjust the map view to the results.

    
     map.on('click', async (e) => {
       const {lng, lat} = e.lngLat;
       const results = await maptilersdk.geocoding.reverse([lng, lat]);
       const map = e.target;
       map.getSource('search-results').setData(results);
       const bounds = results.features.reduce(function (bounds, feature) {
           return bounds.extend(feature.bbox);
       }, new maptilersdk.LngLatBounds(results.features[0].bbox));
       map.fitBounds(bounds);
     });
    
  8. We already have the results on the map, now we will create a list to show the place names found. By clicking on a name we will zoom to the bbox of the place

    
     map.on('click', async (e) => {
       const {lng, lat} = e.lngLat;
       const results = await maptilersdk.geocoding.reverse([lng, lat]);
       const map = e.target;
       map.getSource('search-results').setData(results);
       const bounds = results.features.reduce(function (bounds, feature) {
           return bounds.extend(feature.bbox);
       }, new maptilersdk.LngLatBounds(results.features[0].bbox));
       map.fitBounds(bounds);
       const resultList = results.features.map((feature) => {
         return `<li onClick="map.fitBounds([${feature.bbox}], {maxZoom: 19})">${feature.place_name}</li>`
       });
       document.getElementById('search-results').innerHTML = resultList.join('');
     });
    
  9. Create the html element where to display the list. Add after the element that contains the map

    
     <div id="results">
       <ul id="search-results">
       </ul>
     </div>
    
  10. Create the style of the list. Add the list style to your stylesheet.

    
     #results {
       position: absolute;
       top: 0;
       left: 0;
       width: 30%;
       overflow: auto;
       background: rgba(255, 255, 255, 0.8);
     }
     #results ul {
       list-style-type: none;
       padding: 0;
       margin: 0;
     }
     #results ul li {
       padding: 10px;
       cursor: pointer;
       border-bottom: 1px solid gray;
     }
     #results ul li:hover {
       color: gray;
     }
     #results ul li:last-child{
       border-bottom-width: 0;
     }
    

Learn more

Check out the tutorials on How to search places (geocoding) and How to search places using the geocoder component.

Visit the MapTiler Geocoding API reference for all search options. For example specifying the language of the results, etc.

Reverse geocoding uses the user’s location to search for places

Reverse geocoding uses the user’s location

Examples

Search for places by transforming coordinates into place names.

Geocoding search for POIs near the user's location

Geocoding search for POIs near the user's location

Examples

Filter geocoding search results by specific place types.

Geolocate control how to get the user's location using the GPS

Geolocate control (GPS)

Tutorials

Enable GPS location tracking using the geolocate map control.

Was this helpful?