How to extract Postal code from V3 Google maps API

后端 未结 2 939
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 14:54

I am using the following to get a the lat-lng from a geocode..

  $latitude = $output->results[0]->geometry->location->lat;
  $longitude = $output->         


        
2条回答
  •  逝去的感伤
    2021-02-04 15:41

    You could use the following function to extract any address component:

    function extractFromAdress(components, type){
        for (var i=0; i

    To extract the postal code you call:

    extractFromAdress(results[0].address_components, "postal_code");
    

    But you could also get other interesting info like:

    extractFromAdress(results[0].address_components, "route");
    extractFromAdress(results[0].address_components, "locality");
    extractFromAdress(results[0].address_components, "country");
    

    etc...

提交回复
热议问题