Skip to main content

Overview

GeocodingService is a Swift actor that provides geocoding functionality for city name searches. It uses the Open-Meteo Geocoding API to find cities by name and return their geographic coordinates and metadata.
Source: BreezeApp/Services/GeocodingService.swift:4

Actor Isolation

This service is implemented as an actor to ensure thread-safe access to its methods and properties. All async methods are actor-isolated and can be safely called from any context.

Shared Instance

A singleton instance of the service that can be accessed throughout the application.

Methods

searchCities

Search for cities by name and return matching results with geographic coordinates.
String
required
The city name or partial name to search for. Must be at least 2 characters long.
Returns: [City] - An array of City objects containing:
  • City name
  • Country information
  • Latitude and longitude coordinates
  • Additional metadata (population, timezone, etc.)
Returns an empty array if the query is less than 2 characters. Throws:
  • URLError.badURL - If the URL cannot be constructed
  • URLError.badServerResponse - If the server returns a non-200 status code
  • DecodingError - If the response cannot be decoded
Implementation Details: The method constructs a URL with the following query parameters:
  • name: The search query string
  • count: Maximum number of results (set to 5)
  • language: Language for results (set to “en” for English)
  • format: Response format (set to “json”)
The minimum query length of 2 characters helps prevent unnecessary API calls for single-character searches.

Usage Example

Simple Usage

API Endpoint

The service uses the Open-Meteo Geocoding API:

Source Code

Performance Considerations

Query Debouncing

When implementing search-as-you-type functionality, consider debouncing the search to avoid making excessive API calls:

Error Handling Best Practices

See Also