Class OpenStreetMapGeoCoder

java.lang.Object
com.example.cab302project.OpenStreetMapGeoCoder
All Implemented Interfaces:
IGeocodingService

public class OpenStreetMapGeoCoder extends Object implements IGeocodingService
Implementation of the geocoding service using OpenStreetMap Nominatim API. Provides functionality to convert addresses to coordinates, generate address suggestions, and perform reverse geocoding.
  • Constructor Details

    • OpenStreetMapGeoCoder

      public OpenStreetMapGeoCoder()
  • Method Details

    • geocodeAddress

      public double[] geocodeAddress(String address) throws Exception
      Converts an address into latitude and longitude using the Nominatim API.
      Specified by:
      geocodeAddress in interface IGeocodingService
      Parameters:
      address - the address string to geocode
      Returns:
      a double array where index 0 is latitude and index 1 is longitude
      Throws:
      IllegalArgumentException - if the address is not found
      Exception - if the HTTP request fails
    • getAddressSuggestions

      public List<String> getAddressSuggestions(String query) throws Exception
      Retrieves up to 5 address suggestions for the given query using the Nominatim API.
      Specified by:
      getAddressSuggestions in interface IGeocodingService
      Parameters:
      query - the partial address string to search for
      Returns:
      a list of matching display name strings, or an empty list if the query is too short
      Throws:
      Exception - if the HTTP request fails
    • reverseGeocode

      public String reverseGeocode(double lat, double lon) throws Exception
      Converts latitude and longitude coordinates into a human-readable address using the OpenStreetMap Nominatim reverse geocoding service.
      Specified by:
      reverseGeocode in interface IGeocodingService
      Parameters:
      lat - the latitude coordinate to reverse geocode
      lon - the longitude coordinate to reverse geocode
      Returns:
      a readable address string, or a formatted coordinate string if no address is found
      Throws:
      Exception - if the HTTP request fails