---
title: "How to search places using Leaflet geocoding control | Leaflet | Geocoding"
description: "Search places with MapTiler geocoding control in Leaflet JS."
---

# How to search places using Leaflet geocoding control

This tutorial shows how to search for places using MapTiler geocoding control. The geocoding control facilitate the use of the [MapTiler Geocoding API](https://www.maptiler.com/cloud/geocoding/).

1.  Copy the following code, paste it into your favorite text editor, and save it as a `.html` file.
    
    ```html
    <!DOCTYPE html>
     <html>
       <head>
         <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
         <title>MapTiler Geocoding control</title>
         <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
         <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
         <style>
           #map {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}
         </style>
       </head>
       <body>
         <div id="map">
           <a href="https://www.maptiler.com" style="position:absolute;left:10px;bottom:10px;z-index:999;"><img src="https://api.maptiler.com/resources/logo.svg" alt="MapTiler logo"></a>
         </div>
         <p><a href="https://www.maptiler.com/copyright/" target="_blank" rel="noopener">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener">&copy; OpenStreetMap contributors</a></p>
         <script>
           const key = 'YOUR_MAPTILER_API_KEY_HERE';
           const map = L.map('map').setView([49.2125578, 16.62662018], 14); //starting position
           L.tileLayer(`https://api.maptiler.com/maps/streets-v4/{z}/{x}/{y}.png?key=${key}`,{ //style URL
             tileSize: 512,
             zoomOffset: -1,
             minZoom: 1,
             attribution: "\u003ca href=\"https://www.maptiler.com/copyright/\" target=\"_blank\"\u003e\u0026copy; MapTiler\u003c/a\u003e \u003ca href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\"\u003e\u0026copy; OpenStreetMap contributors\u003c/a\u003e",
             crossOrigin: true
           }).addTo(map);
         </script>
       </body>
     </html>
    ```
    

1.  Install the npm package.
    
    ```bash
    npm install --save leaflet
    ```
    
2.  Include the CSS file.
    
    If you have a bundler that can handle CSS, you can [`import`](#) the CSS or include it with a [`<link>`](#) in the head of the document via the CDN
    
    ```js
    import "leaflet/dist/leaflet.css";
    ```
    
    ```html
    <link href='https://unpkg.com/leaflet@1.9.4/dist/leaflet.css' rel='stylesheet' />
    ```
    
3.  Include the following code in your JavaScript file (Example: main.js).
    
    ```js
    import L from 'leaflet';
        
    const key = 'YOUR_MAPTILER_API_KEY_HERE';
    const map = L.map('map').setView([49.2125578, 16.62662018], 14); //starting position
    L.tileLayer(`https://api.maptiler.com/maps/streets-v4/{z}/{x}/{y}.png?key=${key}`,{ //style URL
      tileSize: 512,
      zoomOffset: -1,
      minZoom: 1,
      attribution: "\u003ca href=\"https://www.maptiler.com/copyright/\" target=\"_blank\"\u003e\u0026copy; MapTiler\u003c/a\u003e \u003ca href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\"\u003e\u0026copy; OpenStreetMap contributors\u003c/a\u003e",
      crossOrigin: true
    }).addTo(map);
    ```
    

1.  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/credentials/api-key/#secure-key) before you publish it.
    
2.  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.
    

1.  Include the geocoder component JavaScript and CSS files in the `<head>` of your HTML file.
    
    ```html
    <script src="https://cdn.maptiler.com/maptiler-geocoding-control/v3.1.0/leaflet.umd.js"></script>
    ```
    
2.  Instantiate the geocoding control and add it to the map.
    
    ```js
    const gc = new maptilerGeocoder.GeocodingControl({ apiKey: key });
     map.addControl(gc);
    ```
    

### Complete source code

```html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Display a map</title>
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
  <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
  <script
    src="https://cdn.maptiler.com/maptiler-geocoding-control/v3.1.0/leaflet.umd.js"></script>
  <style>
    body {
      margin: 0;
      padding: 0;
    }

    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>

<body>
  <div id="map"></div>
</body>
<script>
  const key = 'YOUR_MAPTILER_API_KEY_HERE';

  const map = L.map('map').setView([0, 0], 2);
  const layer = L.tileLayer(`https://api.maptiler.com/maps/streets-v4/{z}/{x}/{y}.png?key=${key}`, {
    tileSize: 512,
    zoomOffset: -1,
    minZoom: 1,
    attribution: "\u003ca href=\"https://www.maptiler.com/copyright/\" target=\"_blank\"\u003e\u0026copy; MapTiler\u003c/a\u003e \u003ca href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\"\u003e\u0026copy; OpenStreetMap contributors\u003c/a\u003e",
    crossOrigin: true
  }).addTo(map);

  const gc = new maptilerGeocoder.GeocodingControl({ apiKey: key });
  map.addControl(gc);
</script>

</html>
```

1.  Install the Geocoding control
    
    ```bash
    npm install --save @maptiler/geocoding-control
    ```
    
2.  Import the Geocoding control component and CSS in your mapping application
    
    ```js
    import { GeocodingControl } from "@maptiler/geocoding-control/leaflet";
    ```
    
3.  Add the geocoder control to the map.
    
    ```js
    const gc = new GeocodingControl({ apiKey: key });
      map.addControl(gc);
    ```
    

### Complete source code

```bash
npm install --save leaflet @maptiler/geocoding-control
```

#### main.js

```js
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import { GeocodingControl } from "@maptiler/geocoding-control/leaflet";

const key = 'YOUR_MAPTILER_API_KEY_HERE';

const map = L.map('map').setView([0, 0], 2);
const layer = L.tileLayer(`https://api.maptiler.com/maps/streets-v4/{z}/{x}/{y}.png?key=${key}`, {
  tileSize: 512,
  zoomOffset: -1,
  minZoom: 1,
  attribution: "\u003ca href=\"https://www.maptiler.com/copyright/\" target=\"_blank\"\u003e\u0026copy; MapTiler\u003c/a\u003e \u003ca href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\"\u003e\u0026copy; OpenStreetMap contributors\u003c/a\u003e",
  crossOrigin: true
}).addTo(map);

const gc = new GeocodingControl({
  apiKey: key,
});
map.addControl(gc);
```

#### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex">
  <title>Display a map</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <div id="map"></div>
  <script type="module" src="./main.js"></script>
</body>
</html>
```

#### style.css

```css
body {
  margin: 0;
  padding: 0;
}

#map {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}
```

## How to position the geocoding control

The `position` property allows you to place the geocoding control in any of the map’s four corners. You can specify `'topleft'`, `'topright'`, `'bottomleft'`, or `'bottomright'` as values to determine its location.

```js
const gc = new maptilerGeocoder.GeocodingControl({ apiKey: key, position: "bottomright" });
map.addControl(gc);
```

```js
const gc = new GeocodingControl({ apiKey: key, position: "bottomright" });
  map.addControl(gc);
```

## Learn more

Visit the [MapTiler Geocoding API reference](https://docs.maptiler.com/cloud/api/geocoding/) for all search options. Like for example specifying the language of the results, etc.

Do you want to see how the geocoding component is made and what options it has? Check the [MapTiler Geocoding control](https://github.com/maptiler/maptiler-geocoding-control) repository.

Are you currently using a different map library? No worries! Learn how to incorporate the geocoding control functionality with **MapTiler SDK JS** ([MapTiler SDK JS Geocoding control](https://docs.maptiler.com/sdk-js/examples/geocoder-component/)), **OpenLayers** ([OpenLayers Geocoding control](https://docs.maptiler.com/openlayers/examples/geocoding-control/)), or **MapLibre GL JS** ([MapLibre GL JS Geocoding control](https://docs.maptiler.com/maplibre/examples/geocoding-control/)).

## Related examples

### [Geolocate control](https://docs.maptiler.com/leaflet/examples/geolocate-user/)

Examples

Get user location using GPS and Leaflet geolocate control.
