Skip to main content

Overview

The Location Search feature allows users to find and select any city worldwide to view its air quality data. It includes real-time search with autocomplete, current location detection via GPS, and a curated list of popular cities.

Implementation

SearchView Component

The search interface is built as a modal sheet with a navigation stack:
SearchView.swift

Features

The search implements debounced autocomplete to avoid excessive API calls:
DashboardViewModel.swift
Debouncing: The search waits 300ms after the user stops typing before making an API request. This reduces server load and provides a smoother user experience.

2. Current Location

The “Use Current Location” button triggers GPS location detection:
DashboardViewModel.swift

Location Manager Delegate

The ViewModel conforms to CLLocationManagerDelegate to handle location updates:
DashboardViewModel.swift
When no search is active, a list of popular cities is displayed:
SearchView.swift

City Model

Cities are represented with comprehensive location data:
City.swift

Display Name Format

The displayName property creates a hierarchical location string:
  • With admin1: “Los Angeles, California, USA”
  • Without admin1: “Paris, France”

Top Cities List

A curated list of major cities worldwide:
City.swift

Geocoding Service

City search is powered by the Open-Meteo Geocoding API via GeocodingService.swift:
GeocodingService.swift

API Response Format

City.swift

User Experience Features

Auto-Focus

The search field automatically gains focus when the sheet opens:

Clear Button

The search field includes a clear button that appears when text is entered:

Search Validation

Search only triggers when at least 2 characters are entered:
Display states:
  • Empty query: Shows popular cities
  • Query < 2 chars: Shows popular cities
  • Query >= 2 chars, no results: Shows “No results found”
  • Query >= 2 chars, has results: Shows search results list

City Selection Flow

  1. User taps a city (from search results or popular cities)
  2. viewModel.selectCity(city) is called
  3. Location name is updated
  4. Search state is cleared
  5. Modal is dismissed
  6. Data is fetched for the new location
DashboardViewModel.swift

Location Permissions

The app handles various location permission states:
1

Not Determined

Request permission from the user
2

Authorized

Request current location
3

Denied/Restricted

Show error message directing user to Settings
Make sure to add the NSLocationWhenInUseUsageDescription key to your Info.plist with an appropriate description of why your app needs location access.

File Locations

  • Component: BreezeApp/Views/Search/SearchView.swift
  • Model: BreezeApp/Models/City.swift
  • Service: BreezeApp/Services/GeocodingService.swift
  • ViewModel: BreezeApp/ViewModels/DashboardViewModel.swift