Recover neighborhood and city only in the Google Maps API by pure PHP coordinates

Asked

Viewed 423 times

-1

I have an application where I need to store only neighborhood and city data in the mysql database, from the results coming in json of the Google Maps api. I’m using the following code:

 $url ='https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $latitude . ',' . $longitude . '&key=';
 $geocode = file_get_contents($url);
 $results = json_decode($geocode, true);
 if($results['status']=='OK'){
$location = $results['results'][0]['address_components'][2]['long_name'];
$location2 = $results['results'][0]['address_components'][3]['long_name'];

}

The problem in this system is that the json result is not standardized, that is, in some moments the neighborhood comes in the 2 position of address_components, in others in the 1 position, in others in the 0 position... I need guidance on how to do it, in pure PHP, without javascript, to always get the neighborhood and the city. The json structure of the standard request is below, but in address_components it does not always follow this order:

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "plus_code": {
            "compound_code": "CWC8+W5 Mountain View, California, United States",
            "global_code": "849VCWC8+W5"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

I really appreciate the help.

1 answer

0

I managed to solve here. Below is the script:

 $url ='https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $latitude . ',' . $longitude . '&key=SuaKeyApiGoogleMaps';
 $geocode = file_get_contents($url);
 $results = json_decode($geocode, true);
 if($results['status']=='OK'){

    $location = $results['results'][0]['address_components'][0]['long_name'];
    $location2 = $results['results'][0]['address_components'][1]['long_name'];
    $location3 = $results['results'][0]['address_components'][2]['long_name'];
    $location4 = $results['results'][0]['address_components'][3]['long_name'];
    $location5 = $results['results'][0]['address_components'][4]['long_name'];
    $type = $results['results'][0]['address_components'][0]['types'];
    $type2 = $results['results'][0]['address_components'][1]['types'];
    $type3 = $results['results'][0]['address_components'][2]['types'];
    $type4 = $results['results'][0]['address_components'][3]['types'];
    $type5 = $results['results'][0]['address_components'][4]['types'];
 }
 if (in_array('sublocality_level_1', $type)) {
     $bairro = $location;
 }
 if (in_array('sublocality_level_1', $type2)) {
     $bairro = $location2;
 }
 if (in_array('sublocality_level_1', $type3)) {
     $bairro = $location3;
 }
 if (in_array('sublocality_level_1', $type4)) {
     $bairro = $location4;
 }
 if (in_array('sublocality_level_1', $type5)) {
     $bairro = $location5;
 }

 if (in_array('administrative_area_level_2', $type)) {
     $cidade = $location;
 }
 if (in_array('administrative_area_level_2', $type2)) {
     $cidade = $location2;
 }
 if (in_array('administrative_area_level_2', $type3)) {
     $cidade = $location3;
 }
 if (in_array('administrative_area_level_2', $type4)) {
     $cidade = $location4;
 }
 if (in_array('administrative_area_level_2', $type5)) {
     $cidade = $location5;
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.