full-path / ridesheet

Google apps script support for a Google Sheets-based demand-responsive trip scheduling and reporting system.
MIT License
5 stars 4 forks source link

Improve Address handling #105

Open NomeQ opened 7 months ago

NomeQ commented 7 months ago

Address parsing is not resilient—on nome-dev branch.

components[i].type contains more information about what sort of address component each is. Check that before assigning.

function buildAddressToSpec(address) {
  try {
    const geocoder = Maps.newGeocoder()
    const result = geocoder.geocode(address)
    if (result["status"] === "OK") {
      const components = result.results[0].address_components
      const addressObj = {
        addressName: "",
        street2: "",
        notes: "",
        formattedAddress: address,
        lat: result.results[0].geometry.location.lat,
        long: result.results[0].geometry.location.lng,
      }
      if (components.length >= 6) {
        addressObj.street = components[0].short_name + " " + components[1].short_name
        addressObj.city = components[2].long_name
        addressObj.state = components[4].short_name
        addressObj.postalCode = components[6].short_name
        addressObj.country = components[5].short_name
      }
      if (components.length === 5) {
        addressObj.street = components[0].long_name
        addressObj.city = components[1].long_name
        addressObj.state = components[3].short_name
        addressObj.country = components[4].short_name
        addressObj.postalCode = ""
      }
      return addressObj
    }
  } catch(e) { logError(e) }
}
NomeQ commented 7 months ago

Related: What to do if we can't extract all required fields? e.g. missing zip code from plus codes, or other address components not present?

keviniano commented 7 months ago

Is the code you quoted in this issue in a branch? I don't see it in nome-dev. https://github.com/full-path/ridesheet/blob/9235b474c7feb7790a5ee04132e065f10b1b5d5e/api_util.js

NomeQ commented 7 months ago

Oh yes, my mistake. It is in ridenoco-trip-request branch!

keviniano commented 7 months ago

OK, I've made a first pass at this. Please look at #105-improve-address-handling.