Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Google Place API : How to get the country code from Google Places API

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 7.01k
    Comment on it

    If you are trying HTML 5 GeoLocation to get longitude and latitude and after then using Google Maps API to get the country code of that longitude/latitude. SO in this tutoril I will tell you a very simple way to get country code from Google places api, you can directly get country code from google place api without without getting latitude and longitude first and then passing to google maps api. You have to just follow the steps below:

    To get the country code, get the short name of the address component with type country:

    <?php
    // for the country, get the country code (the "short name") also
    if (addressType == "country") {
      document.getElementById("country_code").value = place.address_components[i].short_name;
    } 
    
    ?>

     

    <?php
    // This example displays an address form, using the autocomplete feature
    // of the Google Places API to help users fill in the information.
    
    function fillInAddress() {
      // Get the place details from the autocomplete object.
      var place = autocomplete.getPlace();
    
      for (var component in componentForm) {
        document.getElementById(component).value = '';
      }
    
      // Get each component of the address from the place details
      // and fill the corresponding field on the form.
      for (var i = 0; i < place.address_components.length; i++) {
        var addressType = place.address_components[i].types[0];
        if (componentForm[addressType]) {
          var val = place.address_components[i][componentForm[addressType]];
          document.getElementById(addressType).value = val;
        }
        // for the country, get the country code (the "short name") also
        if (addressType == "country") {
          document.getElementById("country_code").value = place.address_components[i].short_name;
        }
      }
    }
    
    var placeSearch, autocomplete;
    var componentForm = {
      locality: 'long_name',
      administrative_area_level_1: 'short_name',
      country: 'long_name',
    };
    
    function initAutocomplete() {
      // Create the autocomplete object, restricting the search to geographical
      // location types.
      autocomplete = new google.maps.places.Autocomplete(
        /** @type {!HTMLInputElement}     */
        (document.getElementById('autocomplete')), {
          types: ['(cities)']
        });
    
      // When the user selects an address from the dropdown, populate the address
      // fields in the form.
      // Get Latitude and longitude
      google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();
        document.getElementById('lat').value = place.geometry.location.lat();
        document.getElementById('lng').value = place.geometry.location.lng();
        fillInAddress();
      });
    }
    google.maps.event.addDomListener(window, 'load', initAutocomplete);
    ?>

     

    Add following css:

    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    #map {
      height: 100%;
    }

     

    <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <div id="locationField">
      <input id="autocomplete" placeholder="Enter your address" type="text" />
    </div>
    <table id="address">
      <tr>
        <td class="label">City</td>
        <td class="wideField" colspan="3">
          <input class="field" id="locality" disabled="true" />
        </td>
      </tr>
      <tr>
        <td class="label">State</td>
        <td class="slimField">
          <input class="field" id="administrative_area_level_1" disabled="true" />
        </td>
      </tr>
      <tr>
        <td class="label">Country</td>
        <td class="wideField" colspan="2">
          <input class="field" id="country" disabled="true" />
        </td>
        <td class="label">Country Code</td>
        <td class="shortField">
          <input class="field" id="country_code" disabled="true" />
        </td>
      </tr>
      <tr>
        <td class="label">latitude</td>
        <td class="wideField" colspan="3">
          <input class="field" id="lat" disabled="true" />
        </td>
      </tr>
      <tr>
        <td class="label">longitude</td>
        <td class="wideField" colspan="3">
          <input class="field" id="lng" disabled="true" />
        </td>
      </tr>
    </table>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: