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.