如何使用谷歌地图API获取阿联酋国家的邮政编码区域?

时间:2016-12-27 14:35:36

标签: javascript jquery ruby-on-rails ruby-on-rails-3

当我得到结果然后在这里我没有得到所选阿联酋地区的区域,因为在阿联酋没有邮政编码/邮政编码。 如果有任何方式获得阿联酋地区的地区,那么建议我。

以下是我的代码:

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('');
  }
}

1 个答案:

答案 0 :(得分:0)

嘿只是抬头看你的问题它不会发生在我身上,但我找到了一些可以帮助你的东西

您需要详细了解区域代码偏差。

获取region list

的列表

要了解更多信息,请访问链接

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