---
title: "How to search places by coordinates (reverse geocoding) | Geocoding | JavaScript maps SDK"
description: "Search places by coordinates using reverse geocoding."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# 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](https://www.maptiler.com/cloud/geocoding/). 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.
    
    ```bash
    npm install --save @maptiler/sdk
    ```
    
2.  Include the CSS file.
    
    If you have a bundler that can handle CSS, you can `import` the CSS
    
    ```js
    import "@maptiler/sdk/dist/maptiler-sdk.css";
    ```
    
    or include it with a `<link />` in the head of the document via the CDN
    
    ```html
    <link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css' rel='stylesheet' />
    ```
    
3.  Include the following code in your JavaScript file (Example: app.js).
    
    ```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](https://cloud.maptiler.com/account/keys/). Make sure to [secure the key](https://docs.maptiler.com/guides/developer/credentials/api-key/#secure-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](https://cloud.maptiler.com/maps/), or create your own to truly differentiate your application.
    
6.  Create the map layer where we will display the search results.
    
    ```js
    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.
    
    ```js
    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
    
    ```js
    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
    
    ```html
    <div id="results">
       <ul id="search-results">
       </ul>
     </div>
    ```
    
10.  Create the style of the list. Add the list style to your stylesheet.
     
     ```css
     #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;
      }
     ```
     

## Basic JavaScript setup

1.  Copy the following code, paste it into your favorite text editor, and save it as a `.html` file.
    
    ```html
    <!DOCTYPE html>
     <html lang="en">
     <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Reverse geocoding</title>
       <script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
       <link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
       <style>
         body { margin: 0; padding: 0; }
         #map { position: absolute; top: 0; bottom: 0; width: 100%; }
       </style>
     </head>
     <body>
       <div id="map"></div>
       <script>
           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
                
           });
       </script>
     </body>
     </html>
    ```
    
2.  Replace `YOUR_MAPTILER_API_KEY_HERE` with [your own API key](https://cloud.maptiler.com/account/keys/). Make sure to [secure the key](https://docs.maptiler.com/guides/developer/credentials/api-key/#secure-key) before you publish it.
    
3.  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](https://cloud.maptiler.com/maps/), or create your own to truly differentiate your application.
    
4.  Create the map layer where we will display the search results.
    
    ```js
    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']
       });
     });
    ```
    
5.  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.
    
    ```js
    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);
     });
    ```
    
6.  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
    
    ```js
    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('');
     });
    ```
    
7.  Create the html element where to display the list. Add after the element that contains the map
    
    ```html
    <div id="results">
       <ul id="search-results">
       </ul>
     </div>
    ```
    
8.  Create the style of the list. Add the list style to your stylesheet.
    
    ```css
    #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;
     }
    ```
    

### Complete source code

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Reverse Geocoding</title>
  <script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
  <link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
  <style>
    body {
      margin: 0;
      padding: 0;
    }
    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
    #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;
    }
  </style>
</head>
<body>
  <div id="map"></div>
  <div id="results">
    <ul id="search-results">
    </ul>
  </div>
  <script>
      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
      });
      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']
        });
      });
      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('');
      });
  </script>
</body>
</html>
```

## Learn more

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

Visit the [MapTiler Geocoding API reference](https://docs.maptiler.com/cloud/api/geocoding/#tag/geocoding/GET/geocoding/{longitude},{latitude}.json) for all search options. For example specifying the language of the results, etc.

## Related examples

### [Reverse geocoding uses the user’s location](https://docs.maptiler.com/sdk-js/examples/geocoding-reverse-json/)

Examples

Search for places by transforming coordinates into place names.

### [Geocoding search for POIs near the user's location](https://docs.maptiler.com/sdk-js/examples/geocoding-filter-types/)

Examples

Filter geocoding search results by specific place types.

### [Geolocate control (GPS)](https://docs.maptiler.com/sdk-js/examples/geolocate-control/)

Tutorials

Enable GPS location tracking using the geolocate map control.
