How to sync the map with a list of places

This tutorial shows how to sync the map with a list of places.

This tutorial will guide you through the process of creating a compilation of locations and synchronizing it with a map. The map will display markers for each of these locations. By following these steps, you will be able to generate a list of places where selecting an item from the list will automatically select the pin on the map. Similarly, when making changes to the map view, the list of places will be updated to only show the locations that are currently visible on the map. Additionally, selecting a place on the map will highlight and select the corresponding item on the list.

Download the shoe sample icon and the selected shoe sample icon, The icon is from the Maps Icons Collection. Download the shoe shops GeoJSON sample data

  1. Copy the following code, paste it into your favorite text editor, and save it as a .html file.

  2. Install the npm package.

  3. 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

  4. Include the following code in your JavaScript file (Example: app.js).

  5. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

  6. 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.

  7. We are going to modify the structure of the web page to have two main elements: a sidebar (aside) where we will show the list of shoe stores and the main area (main) where we will display the map.

  8. Add the CSS classes to create the simple Flexbox Layout for the Sidebar + Main Content Area

  9. Reload the page. Now you should see the app in your browser.

    Flexbox simple layout sidebar + main

  10. Add an event handler for the map load event. You will add code to create a GeoJSON source and a vector layer in this handler. We will also add other map event handlers.

  11. Add a custom images to the map. You can use any image or download the shoe image and the selected shoe image we used in the tutorial. In the example, these images are in the same folder as the index.html file

  12. Create GeoJSON source. The following snippet creates GeoJSON source hosted on MapTiler Cloud (check out the How to Upload GeoJSON to MapTiler Cloud tutorial). Publish the dataset and copy the link to the GeoJSON. Download the shoe shop GeoJSON sample data. In this example we will add a random image (in the photo property) to each of the map elements.

  13. Add the vector layer. In this example, we will use the shoe image to display a point layer using a custom PNG icon as a pin.

  14. Now you should see the map with pins.

    Map with shoe shop pins

  15. Change the cursor to a pointer when the mouse is over the points layer.

  16. Create the list with the elements that are displayed on the map.

  17. Update the list when the map view is changed. With this event, we will have the map view and the list synchronized. Every time the view is updated (zoomed, panned) the “Search in this area” button is displayed. This will allow us to refresh the list and only show the elements that appear on the map.

  18. Get the point layer elements when the user clicks on the map. With the click event, the user will be able to select an element on the map. This will allow us to select the same item in the list.

  19. We are done with the functions that are executed when the map is loaded. Now we will create the functions outside of the load event handler.

  20. Get the elements that are visible in the map view.

  21. Select a map element when the user clicks on it.

  22. Create a list of places.

  23. Create the list style. Add the list style to your stylesheet.

  24. With everything done up until now, you should be able to see your map and the list of places in your browser.

    Map with a list of places

  25. Next, we are going to add some events to link the list with the map. These events will ensure that when a list item is clicked on, the corresponding marker on the map is selected and centered on the map view.

  26. We have already made it so that selecting an item from the list selects it on the map. Now we will do the other way, when selecting an element on the map it is selected in the list.

  27. At this point we already have the list linked and synchronized with the map. Now we will create some functions to toggle the selected elements so that we have only one element selected in both the list and the map.

  28. Finally, we need to show the “Search in this area” button when the map view is updated and update the list when the user clicks the button.

  29. Congratulations! You have your map with place markers synchronized with the list of places.

Learn more

Related examples

An extension of MapLibre GL JS