Spatial Analysis in 2025: Key Trends Report
| Download Now
CARTO
Platform
Overview
Visualization
Analytics
App Development
Data Enrichment
AI Agents
Enterprise & Security
Pricing
CARTO for
Google Cloud
AWS
Azure
Snowflake
Databricks
Oracle
Solutions
By Industry
By Use Case
By Role
Telecoms
Icon/indus/Insurance-menu
Icon/indus/Insurance-menu-mobile
Insurance
Logistics
Real Estate
Financial Services
Retail
Marketing & Advertising
Mobility
All industries
Network Deployment
Catastrophe Modeling
Fraud Detection
Market Analysis
Environmental Management
Site Selection
Geomarketing
Data Monetization
IoT Analytics
Supply Chain Optimization
Healthcare Analytics
Territory Planning
All use cases
Data Analyst
Data Scientist
GIS Professional
Developer
Customers
Resources
Learn
Documentation
CARTO Academy
Blog
Reports
Glossary
Connect
Events
Partners
Webinars
Log inRequest a demo
Try for free
2050-01-01
1990-01-01
"ES", "GB"

This content applies to a previous version of CARTO

In October 2021 we released a new version of our platform. You can find the latest documentation at docs.carto.com

Back to Help Center

How can you search your own CARTO dataset in a CARTO.js map?

Overview

The example demonstrates creating a map with a dropdown menu listing country names from the ne_adm0_europe CARTO dataset’s admin field. Selecting a country from the dropdown adjusts the map’s zoom level and center to fit the selected country’s bounding box.

Implementation Steps

  1. Create a CARTO.js map with one layer
  2. Build a populateDropDown() function that uses the CARTO SQL API to retrieve geometry and names from the dataset, then populates the HTML <select> dropdown
  3. Make another SQL API request to retrieve data for the selected country and use mapping library methods to fit the map bounds

CARTO SQL API Request

The implementation uses the JavaScript Fetch API to query CARTO’s SQL API. Alternative libraries like Axios or jQuery can be used for broader browser compatibility.

Leaflet Approach

Uses L.geoJson() to convert API response data, then applies getBounds() and fitBounds() methods:

document.getElementById('selectDrop').addEventListener("change", function (e) {
    input = e.currentTarget.selectedOptions[0].attributes[0].value;
    return fetch(`https://cartojs-test.carto.com/api/v2/sql?format=geojson&q=SELECT * FROM ne_adm0_europe where admin Ilike '${input}'`)
    .then((resp) => resp.json())
    .then((response) => {
        geojsonLayer = L.geoJson(response)
        map.fitBounds(geojsonLayer.getBounds());
    })
});

Google Maps Approach

Retrieves bounding box coordinates using PostGIS methods (ST_Envelope, ST_Xmax, ST_Ymax, ST_Xmin, ST_Ymin), then applies Google Maps coordinate objects:

document.getElementById('selectDrop').addEventListener("change", function (e) {
    input = e.currentTarget.selectedOptions[0].attributes[0].value;
    return fetch(`https://cartojs-test.carto.com/api/v2/sql?format=geojson&q=
        WITH bbox as (
            SELECT ST_Envelope(the_geom) as the_geom
            FROM ne_adm0_europe
            WHERE admin Ilike '${input}'
        )
        SELECT the_geom, ST_Xmax(the_geom) as xmax,
                ST_Ymax(the_geom) as ymax,
                ST_Xmin(the_geom) as xmin,
                ST_Ymin(the_geom) as ymin
        FROM bbox`)
    .then((resp) => resp.json())
    .then((response) => {
        coordinates = response['features'][0].properties
        let sw = new google.maps.LatLng(coordinates.ymin, coordinates.xmin);
        let ne = new google.maps.LatLng(coordinates.ymax, coordinates.xmax);
        bounds = new google.maps.LatLngBounds(sw, ne)
        map.fitBounds(bounds);
    })
});
CARTO
carto-logo
  • Platform

  • Overview

  • Visualization

  • Analytics

  • App Development

  • Data Enrichment

  • Security & Governance

  • Gen AI

  • Pricing

  • Solutions

  • Data Analyst

  • Developer

  • Data Scientist

  • GIS Professional

  • By Industry

  • By Use Case

  • Resources

  • Customer Stories

  • Blog

  • Glossary

  • Documentation

  • Academy

  • Reports

  • Events

  • Webinars

  • Partners

  • Company

  • About us

  • Newsroom

  • Careers

  • Brand

  • Grants

Unlock the power of spatial analysis.

youtube logofacebook logotwitter logolinkedin logo

© CARTO 2024

Terms

Privacy Notice

Whistleblower Form