amazon-web-servicesamazon-location-service

AWS Location geocode city by zip


In my Java application the user enters their zip code and city to determine the weather in the user's region. If the city is not specified, I need to find it using the entered zip code (the largest city in the state). I use Google geocoding, which is quite expensive.

I'd like to be able to lookup cities information based on an zip code, such as postcode localities in Google Geocoding. Does AWS Location provide this service?


Solution

  • Yes, it does. Use Amazon Location's Places API for your use case, specifically the SearchPlaceIndexForText action. With that, you can search for place names, addresses, and business names and get two things in return: geographic coordinates, and address components. Address components is what you need, which gives you things like country, state, city, postal code, etc. You can also store the results if you want to; checkout the Data storage option when creating your place index resource.

    Here is an example request (using AWS CLI) and response using ZIP codes as the input:

    aws location search-place-index-for-text \
    --index-name HerePlace \
    --text "98121" \
    --filter-countries "USA" \
    --max-results 1
    
    {
        "Results": [
            {
                "Place": {
                    "Country": "USA",
                    "Geometry": {
                        "Point": [
                            -122.34468,
                            47.61578
                        ]
                    },
                    "Interpolated": false,
                    "Label": "98121, Seattle, WA, United States",
                    "Municipality": "Seattle",
                    "PostalCode": "98121",
                    "Region": "Washington",
                    "SubRegion": "King",
                    "TimeZone": {
                        "Name": "America/Los_Angeles",
                        "Offset": -28800
                    }
                }
            }
        ],
        "Summary": {
            "DataSource": "Here",
            "FilterCountries": [
                "USA"
            ],
            "MaxResults": 1,
            "ResultBBox": [
                -122.34468,
                47.61578,
                -122.34468,
                47.61578
            ],
            "Text": "98121"
        }
    }
    

    Hope this helps.