Question: How to get region in place of zipcode for UAE countries using google maps API?

Question

How to get region in place of zipcode for UAE countries using google maps API?

Answers 1
Added at 2016-12-27 15:12
Tags
Question

When I get results then in this i am not getting region of the selected UAE area because in UAE there is no postal code/zipcode. if any way to get region for UAE areas then suggest me.

Below is my code:

var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  administrative_area_level_1: 'short_name',
  locality: 'long_name',
  postal_code: 'short_name'
};

var formComponentIds = {
  street_number: 'user_add_street',
  route: 'user_add_street',
  administrative_area_level_1: 'user_add_state',
  locality: 'user_add_city',
  postal_code: 'user_add_zipcode'
};

function initAutocomplete () {
  autocomplete = new google.maps.places.Autocomplete((document.getElementById('user_add_street')),{types: ['geocode']});
    autocomplete.addListener('place_changed', fillInAddress);
  $(document).on('focusout', '#pickupStreet', function() {
    var firstResult = $(".pac-container .pac-item:first").text();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({"address":firstResult }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var lat = results[0].geometry.location.lat(),
        lng = results[0].geometry.location.lng(),
        placeName = results[0].address_components[0].long_name,
        latlng = new google.maps.LatLng(lat, lng);
        $('#pickup_latitude').val(lat);
        $('#pickup_longitude').val(lng); 
        debugger          
        for (var i = 0; i < results[0].address_components.length; i++) {
          if( results[0].address_components[i].types[0] == 'route'){
              $('#pickupStreet').val(results[0].address_components[i-1].long_name + " " + results[0].address_components[i].long_name);
          }
          else if (results[0].address_components[i].types[0] == 'locality'){
            $('#pickupCity').val(results[0].address_components[i].long_name);
          }
          else if (results[0].address_components[i].types[0] == 'administrative_area_level_1'){
            $('#pickupState').val(results[0].address_components[i].long_name);
          }
          // else if (results[0].address_components[i].types[0] == 'postal_code'){
          //   $('#pickupZipcode').val(results[0].address_components[i].long_name);
          // }
          else{
            console.log();
          }
        }
        $('#user_customer_detail_attributes_zipcode').val($('#pickupZipcode').val());
      }
    });
  });

}

function fillInAddress () {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();
  $('#pickup_latitude').val(place.geometry.location.lat());
  $('#pickup_longitude').val(place.geometry.location.lng());

  if (place.address_components) {
    for (var component in componentForm) {
      componentId = formComponentIds[component];
      document.getElementById(componentId).value = '';
      document.getElementById(componentId).disabled = false;
    }

    // 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]];
        if (formComponentIds[addressType] == 'user_add_street') {
          var street = document.getElementById(formComponentIds[addressType]).value;
          document.getElementById(formComponentIds[addressType]).value = street + ' ' + val;
        } else {
          document.getElementById(formComponentIds[addressType]).value = val;
        }
      }
    }
    $('#user_add_street').val(place.name.split(','));
    document.getElementById('user_add_apartment_number') = '';
  } else {
    splitted_address = place.name.split(',');
    street = splitted_address[0];
    city = splitted_address[1];
    state = splitted_address[2];
    $('#pickupStreet').val(street);
    $('#pickupCity').val(city);
    $('#pickupState').val(state);
    $('#pickupZipcode').val('');
  }
}
Answers to

How to get region in place of zipcode for UAE countries using google maps API?

nr: #1 dodano: 2016-12-27 16:12

Hey just looked up to your problem its not happens to me but i found something which may help you on this

You need to explore more about Region Code Biasing.

For get the list of the region list

To get more about please go through to link

https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRegionCodes

Source Show
◀ Wstecz