Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Error’d: Pickup Sticklers

      September 27, 2025

      From Prompt To Partner: Designing Your Custom AI Assistant

      September 27, 2025

      Microsoft unveils reimagined Marketplace for cloud solutions, AI apps, and more

      September 27, 2025

      Design Dialects: Breaking the Rules, Not the System

      September 27, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 2025

      Cailabs secures €57M to accelerate growth and industrial scale-up

      September 12, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

      September 28, 2025
      Recent

      Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

      September 28, 2025

      Mastering PHP File Uploads: A Guide to php.ini Settings and Code Examples

      September 28, 2025

      The first browser with JavaScript landed 30 years ago

      September 27, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured
      Recent
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Invoke the Mapbox Geocoding API to Populate the Location Autocomplete Functionality

    Invoke the Mapbox Geocoding API to Populate the Location Autocomplete Functionality

    August 22, 2025

    While working on one of my projects, I needed to implement an autocomplete box using Mapbox Geocoding APIs in a React/Next.js application. The goal was to filter a list of hospitals based on the selected location. The location results from the API include coordinates, which I compared with the coordinates of the hospitals in my list.

    The API returns various properties, including coordinates, under the properties section (as shown in the image below). These coordinates (latitude and longitude) can be used to filter the hospital list by matching them with the selected location.

    Mapboxresultproperties

    The API requires an access token, which can be obtained by signing up on the Mapbox platform. You can refer to the Geocoding API documentation for more details. The documentation provides a variety of APIs that can be used depending on your specific requirements.

    Below are some example APIs taken from the same link.

    # A basic forward geocoding request
    # Find Los Angeles
    
    curl "https://api.mapbox.com/search/geocode/v6/forward?q=Los%20Angeles&access_token=YOUR_MAPBOX_ACCESS_TOKEN"
    
    # Find a town called 'Chester' in a specific region
    # Add the proximity parameter with local coordinates
    # This ensures the town of Chester, New Jersey is in the results
    
    curl "https://api.mapbox.com/search/geocode/v6/forward?q=chester&proximity=-74.70850,40.78375&access_token=YOUR_MAPBOX_ACCESS_TOKEN"
    
    # Specify types=country to search only for countries named Georgia
    # Results will exclude the American state of Georgia
    
    curl "https://api.mapbox.com/search/geocode/v6/forward?q=georgia&types=country&access_token=YOUR_MAPBOX_ACCESS_TOKEN"
    
    # Limit the results to two results using the limit option
    # Even though there are many possible matches
    # for "Washington", this query will only return two results.
    
    curl "https://api.mapbox.com/search/geocode/v6/forward?q=Washington&limit=2&access_token=YOUR_MAPBOX_ACCESS_TOKEN"
    
    # Search for the Place feature "Kaaleng" in the Ilemi Triangle. Specifying the cn worldview will return the country value South Sudan. Not including leaving the worldview parameter would default to the us worldview and return the country value Kenya.
    
    curl "https://api.mapbox.com/search/geocode/v6/forward?q=Kaaleng&worldview=cn&access_token=YOUR_MAPBOX_ACCESS_TOKEN"

    The implementation leverages React hooks along with state management for handling component behavior and data flow.

    How to Create an Autocomplete Component in React

    1. Create a React component.
    2. Sign up and apply the access token and API URL to the constants.
    3. Create a type to bind the structure of the API response results.
    4. Use the useEffect hook to invoke the API.
    5. Map the fetched results to the defined type.
    6. Apply CSS to style the component and make the autocomplete feature visually appealing.
    #constants.ts
    
    export const APIConstants = {
      accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
      geoCodeSearchForwardApiUrl: 'https://api.mapbox.com/search/geocode/v6/forward',
      searchWordCount: 3,
    };
    
    #LocationResultProps.ts
    
    type Suggetions = {
      properties: {
        feature_type: string;
        full_address: string;
        name: string;
        name_preferred: string;
        coordinates: {
          longitude: number;
          latitude: number;
        };
      };
    };
    export type LocationResults = {
      features: Array<Suggetions>;
    };
    
    #Styles.ts
    
    export const autoComplete = {
      container: {
        width: '250px',
        margin: '20px auto',
      },
      input: {
        width: '100%',
        padding: '10px',
        fontSize: '16px',
        border: '1px solid #ccc',
        borderRadius: '4px',
      },
      dropdown: {
        top: '42px',
        left: '0',
        right: '0',
        backgroundColor: '#fff',
        border: '1px solid #ccc',
        borderTop: 'none',
        maxHeight: '150px',
        listStyleType: 'none',
        padding: '0',
        margin: '0',
        zIndex: 1000,
      },
      item: {
        padding: '5px',
        cursor: 'pointer',
        borderBottom: '1px solid #eee',
      },
    };
    
    
    #LocationSearchInput.tsx
    
    import React, { useEffect, useState } from 'react';
    import { APIConstants } from 'lib/constants';
    import { autoComplete } from '../Styles';
    import { LocationResults } from 'lib/LocationResultProps';
    
    export const Default = (): JSX.Element => {
      const apiUrlParam: string[][] = [
        //['country', 'us%2Cpr'],
        ['types', 'region%2Cpostcode%2Clocality%2Cplace%2Cdistrict%2Ccountry'],
        ['language', 'en'],
        //['worldview', 'us'],
      ];
    
      const [inputValue, setInputValue] = useState<string>('');
      const [results, setresults] = useState<LocationResults>();
      const [submitted, setSubmitted] = useState<boolean>(false);
    
      // When the input changes, reset the "submitted" flag.
      const handleChange = (value: string) => {
        setSubmitted(false);
        setInputValue(value);
      };
      const handleSubmit = (value: string) => {
        setSubmitted(true);
        setInputValue(value);
      };
    
      // Fetch results when the input value changes
      useEffect(() => {
        if (inputValue.length < APIConstants?.searchWordCount) {
          setresults(undefined);
          return;
        }
        if (submitted) {
          return;
        }
        const queryInputParam = [
          ['q', inputValue],
          ['access_token', APIConstants?.accessToken ?? ''],
        ];
    
        const fetchData = async () => {
          const queryString = apiUrlParam
            .concat(queryInputParam)
            .map((inner) => inner.join('='))
            .join('&');
          const url = APIConstants?.geoCodeSearchForwardApiUrl + '?' + queryString;
    
          try {
            const response: LocationResults = await (await fetch(url)).json();
            setresults(response);
            console.log(response);
          } catch (err: unknown) {
            console.error('Error obtaining location results for autocomplete', err);
          }
        };
    
        fetchData();
      }, [inputValue]);
    
      return (
        <div>
          <div style={autoComplete.container}>
            <input
              style={autoComplete.input}
              onChange={(e) => handleChange(e.target?.value)}
              value={inputValue}
              placeholder="Find Location"
            />
    
            {inputValue &&
              !submitted &&
              results?.features?.map((x) => {
                return (
                  <ul style={autoComplete.dropdown}>
                    <li style={autoComplete.item}>
                      <span onClick={() => handleSubmit(x?.properties?.full_address)}>
                        {x?.properties?.full_address}
                      </span>
                    </li>
                  </ul>
                );
              })}
          </div>
        </div>
      );
    };
    

    Finally, we can search for a location using a zip code, state, or country.

    Recording 20250520 135312 (1)

     

    Additionally, the reverse geocoding API is used similarly, requiring only minor adjustments to the parameters and API URL. The location autocomplete box offers a wide range of use cases. It can be integrated into user forms such as registration or contact forms, where exact location coordinates or a full address need to be captured upon selection. Each location result includes various properties. Based on the user’s input, whether it’s a city, ZIP code, or state, the autocomplete displays matching results.

     

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticlePart 2: Implementing Azure Virtual WAN – A Practical Walkthrough
    Next Article How to Automate Flutter Testing and Builds with GitHub Actions for Android and iOS

    Related Posts

    Development

    Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

    September 28, 2025
    Development

    Mastering PHP File Uploads: A Guide to php.ini Settings and Code Examples

    September 28, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    How to Choose the Best Programming Languages, Libraries, and Patterns

    Development

    Meta to Train AI on E.U. User Data From May 27 Without Consent; Noyb Threatens Lawsuit

    Development

    For the Love of Code: a summer hackathon for joyful, ridiculous, and wildly creative projects

    News & Updates

    FBI: Play ransomware breached 900 victims, including critical orgs

    Security

    Highlights

    Development

    Malware Attack Targets World Uyghur Congress Leaders via Trojanized UyghurEdit++ Tool

    April 29, 2025

    In a new campaign detected in March 2025, senior members of the World Uyghur Congress…

    LLM Reasoning Benchmarks are Statistically Fragile: New Study Shows Reinforcement Learning RL Gains often Fall within Random Variance

    April 15, 2025

    Human Biases – How Smart Teams Can Still Make Dumb Decisions

    May 29, 2025

    Centralize HTTP Client Configuration with Laravel’s globalOptions Method

    April 29, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.