Вопрос

How can I convert city or state name to postal code in javascript or angularjs using Geocoder API or some other way? From the user input I want to convert it.

Это было полезно?

Решение 2

Here is factory example:

feederliteModule.factory('maps', ['$q', function ($q) {
var geocoder = new google.maps.Geocoder();
return {
    geocode: function (address) {
        var deferred = $q.defer();

        geocoder.geocode({
            'address': address
        }, function (results, status) {
            deferred.resolve(results);
        // Should also reject if AJAX errors.
        });

        return deferred.promise;
    }
  };
}]);

where address is input.

The output should be json view where you can find zip code:

 ...
 {
    "long_name": "168751",
    "short_name": "168751",
    "types": [
      "postal_code"
    ]
  }
   ...

Now you can call it from controller like:

 maps.geocode(address)

Другие советы

Thank you all. I solved it.....:) Here is the code:

function getCity( options) {

var geocoder = new google.maps.Geocoder(),
    request;

if( options.latitude ) {

    request= { 'latLng': new google.maps.LatLng( options.latitude, options.longitude ) };

} else {

    request= { 'address': options.address };

};

geocoder.geocode( request, function( results, status ) {

    if ( status == google.maps.GeocoderStatus.OK ) {

        console.log( results );

       geocoder.geocode( request, function( results, status ) {

        if ( status == google.maps.GeocoderStatus.OK ) {

            console.log( results );


            var latitude = results[0].geometry.location.jb;
            var longitude = results[0].geometry.location.kb;
                var  latlng = new google.maps.LatLng(latitude, longitude)

                alert(latlng);

                geocoder.geocode({'latLng': latlng}, function(results, status) {

                    //no result, check addresses
                    for( var resultIndex = 0; resultIndex < results.length; resultIndex++ ) {

                        var addresses = results[resultIndex].address_components;

                        for( var addressIndex = 0; addressIndex < addresses.length; addressIndex++ ) {

                            var types = addresses[addressIndex].types;

                            for( var typeIndex = 0; typeIndex < types.length; typeIndex++ ) {

                                if( types[typeIndex] == 'postal_code' ) {
                                    alert(addresses[addressIndex].long_name) ;
                                    return;

                                };

                            };

                        };

                    };


                });


    } else {

        console.log( 'error: ' + status );
       // complete();

    };

});

};

Google has a slick API for that. Check out this Stack Overflow thread on getting zip code from a city name using the GoogleAPI: Easiest way to get Zip Code from City Name

Of course, a ZIP code is not necessarily related to a place, but a delivery route. The USPS controls ZIP codes and rearranges them as needed for efficiency in delivering the mail. Many people have come to expect that a ZIP code exactly represents a certain city or area in a city, but it's not static. Of the 42,000 5-digit ZIP codes in the United States, approximately 4% of them change each month, so wherever you get your city/state/zip data, make sure it is updated by the USPS on a monthly basis.

In addition to googlemaps there are many commercial services that provide what you're looking for. A quick google search for "zip code api" will do the trick.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top