Plot Postal Code Boundaries Using a Range

Balasaheb Molawade 136 Reputation points
2024-06-19T12:23:06.09+00:00

Hi,

We are using the Bing Maps API to fetch the boundaries of entered postal codes. We have a requirement to handle a range of postal codes, for example, from 10301 to 10312. When user enter 10301- 10312 we need to fetch the boundaries of all postal codes within this range, which means we need to plot 12 boundaries on the map.

Is there any API or method in Bing Maps to achieve this requirement?

Thanks!

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
734 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. rbrundritt 18,591 Reputation points Microsoft Employee
    2024-06-19T18:54:27.6066667+00:00

    There is no API for retrieving a range of postal codes. At best you can try and retrieve a boundary for each possible postal code (assuming all numbers between 10301 and 10312). The Spatial data module in Bing Maps Web SDK has a feature for retrieving multiple boundaries, but it is just making a request for each individual boundary behind the scenes (sample).

    Note, since there is the risk of a postal code in a range not existing, but being geocoded as something else, it would be good to retrieve the metadata with the boundary and compare it with the input. Here is an example for your scenario using the Web SDK:

    var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
        /* No need to set credentials if already passed in URL */
        center: new Microsoft.Maps.Location(40.57, -74.12),
        zoom: 11
    });
    //Create an array of locations to get the boundaries of
    var zipCodes = [];
    
    var startZipCode = 10301;
    var endZipCode = 10312;
    
    for(var i = startZipCode; i <= endZipCode; i++) {
    	var zip = i + '';
    	
    	//Check to see if it is 5 characters long as expected of US zip codes.
    	//May be shorter if there are leading zeros for the zip code. Re-add them.
    	while(zip.length < 5){
    		zip = '0' + zip;
    	}
    	
    	zipCodes.push(zip);
    }
    
    console.log(zipCodes);
    
    var geoDataRequestOptions = {
        entityType: 'Postcode1',
        getAllPolygons: true,
    	getEntityMetadata: true //Get metadata for the boundaries.
    };
    Microsoft.Maps.loadModule('Microsoft.Maps.SpatialDataService', function () {
        //Use the GeoData API manager to get the boundary
        Microsoft.Maps.SpatialDataService.GeoDataAPIManager.getBoundary(zipCodes, geoDataRequestOptions, map, function (data) {
    
    		if (data.results && data.results.length > 0 && data.location) {
    			//Verify that the result name matches with one of the zip codes being requested.
    			var locationIdx = zipCodes.indexOf(data.location);
    			if(locationIdx != -1){ 
    				console.log('Found: ' + data.location);
    				
                	map.entities.push(data.results[0].Polygons);
    				
    				//Optionally remove the zip code from the array to prevent duplicate polygons from being added.
    				zipCodes.splice(locationIdx, 1);				
    			} else {
    				console.log('Duplicate result for: ' + data.location)
    			}
            }
        }, null, function errCallback(callbackState, networkStatus, statusMessage) {
    		console.log('No result for: ' + callbackState);
        });
    });
    

    I just ran this in the interactive SDK here.

    One issue I did notice is that I would often get an error for one of the zip codes, but when I reran it again, a different one would error. I suspect this might be related to rate limiting and require making the requests directly to the REST services using fetch and some throttling logic as the current code requests all polygons at once. You may even want to consider some retry logic for the failed ones.

    1 person found this answer helpful.

  2. IoTGirl 3,131 Reputation points Microsoft Employee
    2024-06-19T19:06:18.64+00:00

    Correct. There is no API to return multiple postal codes by design. Note that depending on the context for such a call, the result set could be incredibly large.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.