0

I have two fields in a table, one is city and other is address. I have to find whether address lies inside city or not. How can i find this.

for fetching lat and lng of city

 $city_lat_lng =json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address={$ogl_city}&sensor=false"));
 $city_lat = $city_lat_lng->results[0]->geometry->location->lat;
 $city_long = $city_lat_lng->results[0]->geometry->location->lng;

for fetching lat and lng of address

$address_lat_lng = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address={$ogl_address}&sensor=false"));
$address_lat = $address_lat_lng->results[0]->geometry->location->lat;
$address_long = $address_lat_lng->results[0]->geometry->location->lng;

In the above code, we are able to find lat long of both address and city. But don't get any proper idea of how to find whether a particular address lies inside a specific city. Both city and address dynamic here. I am new in Laravel. searched on google also but not get any proper solutions.

Community
  • 1
  • 1

1 Answers1

1

You can use address_components from the API response to get the city:

$response = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address={$ogl_city}&sensor=false"));
$firstOption = $response[0];

for($i=0;$i<count($firstOption['address_components']);$i++)
{
    if (in_array ("administrative_area_level_1", $firstOption['address_components'][$i]['types']))
    {
        echo "city = ". $firstOption['address_components'][$i]['long_name'].PHP_EOL;
    }
}

administrative_area_level_1 is a type of administrative area, check which types match your idea of a city here: https://developers.google.com/places/supported_types

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26