Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

murilo's avatar
Level 10

Google Places API auto complete, better way to synchronize with my database

I am working here in some projects . I search for an address in auto complete Google Places API .

The API brings me back - the city name and the neighborhood name of this address that I selected. The API as well give me the place_id , that is an ID of this address that I am looking for .

I wold like to know if I can get as well an ID of city and id of the Neighborhood at the moment that I select the full address .

because after I get the city name and the neighborhood name , I must compare in my database those cities and those neighborhood with the same name .

If I could have for each city and each neighborhood an google_place_id column in my database wold be easiest to compare it..

Does have any way to get a place id of the city and neighborhood selecting a full address ?

0 likes
1 reply
LaryAI's avatar
Level 58

Unfortunately, it is not possible to get the place ID of a city or neighborhood directly from the Google Places API autocomplete response. However, you can use the place ID of the selected address to retrieve additional details about the place, including its address components such as the city and neighborhood.

Here's an example of how you can use the Google Places API Place Details request to retrieve the city and neighborhood of a selected address:

// Assuming you have the place ID of the selected address
$placeId = '...';

// Send a Place Details request to retrieve additional details about the place
$placeDetails = file_get_contents("https://maps.googleapis.com/maps/api/place/details/json?key=YOUR_API_KEY&place_id=$placeId");

// Parse the JSON response
$placeDetails = json_decode($placeDetails, true);

// Extract the city and neighborhood from the address components
$addressComponents = $placeDetails['result']['address_components'];
$city = null;
$neighborhood = null;
foreach ($addressComponents as $component) {
    if (in_array('locality', $component['types'])) {
        $city = $component['long_name'];
    }
    if (in_array('neighborhood', $component['types'])) {
        $neighborhood = $component['long_name'];
    }
}

// Now you can use the city and neighborhood to compare with your database

Note that you will need to replace YOUR_API_KEY with your actual Google Places API key. Also, keep in mind that the address components returned by the API may vary depending on the location and the type of address.

Please or to participate in this conversation.