Welcome Guest 

Show/Hide Header

Welcome Guest, posting in this forum requires registration.

Pages: [1]
Author Topic: Bing Maps REST Geocoding
Posts: 4
Post Bing Maps REST Geocoding
on: June 19, 2011, 17:09

Last week I had an inquiry from a potential client for geocoding 10000 address in US.

This is nothing new or complicated for me but after 200 test geocoding records he sad that the accuracy of

geocoding which suggested is not enough. It was usual routable geocoding.
So after three days of hard work now I can honestly say that I geocoding up to 50000 records per day.

And,you know- the beauty with REST is that you simply need to build URL queries and handle the JSON or XML

responses. It’s quite beautiful actually and it’s essentially the way the WWW was architected, so the learning

curve will be fairly flat.

Below you will find the sources of knowledge and source code.







Sourse Code:

The XML Feed:

 <GeocodeEntity Id="001"
 <GeocodeRequest Culture="en-US">
  <Address AddressLine="291 Broadway" AdminDistrict="NY" Locality="New York"
    PostalCode="10007" />
 <GeocodeEntity Id="002"
 <GeocodeRequest Culture="en-US">
  <Address AddressLine="599 Broadway" AdminDistrict="NY" Locality="New York" />
  <ConfidenceFilter MinimumConfidence="High" xmlns="" />
 <GeocodeEntity Id="003"
 <GeocodeRequest Culture="en-US" Query="Empire State Building" />

The Code:

 <title>Using the Bing Spatial Data Geocode Dataflow API</title>

$key = "Bing Maps Key";
$url = "


// STEP 1 - Create a geocode job

// Get the contents of an XML data file
$myfile = "geocodeFeed.xml";
$data = file_get_contents($myfile);

// Call custom function to generate an HTTP request and get back an HTTP response
$response = do_post_request($url, $data);

// This function constructs and sends an HTTP request with a provided URL and data, and returns an HTTP 

response object
// This function uses the php_http extension
function do_post_request($url, $data, $optional_headers = null) {
 $request = new HttpRequest($url, HttpRequest::METH_POST);
 $response = $request->send();
 return $response->getBody();

// Convert the response body into an XML element so we can extract data
$responseBody = new SimpleXMLElement($response);

// Get data (such as job id, status description, and job status) from the response
$statusDescription = $responseBody->StatusDescription;
$jobId = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Id;
$jobStatus = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Status;

echo "Job Created:<br>";
echo " Request Status: ".$statusDescription."<br>";
echo " Job ID: ".$jobId."<br>";
echo " Job Status: ".$jobStatus."<br><br>";

// STEP 2 - Get the status of geocode job(s)

// Call the API to determine the status of all geocode jobs associated with a Bing Maps key
echo "Checking status until complete...<br>";
while ($jobStatus != "Completed") {

 // Wait 5 seconds, then check the job’s status

 // Construct the URL to check the job status, including the jobId
 $checkUrl = "".$jobId."?&output=xml&key=".$key;
 $checkResponse = file_get_contents($checkUrl);
 $responseBody = new SimpleXMLElement($checkResponse);

 // Get and print the description and current status of the job
 $jobDesc = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Description;
 $jobStatus = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Status;

 echo $jobDesc." - ".$jobStatus."<br>";

echo "checkUrl: " . $checkUrl . "<br>";

// STEP 3 - Obtain results from a successfully geocoded set of data


//Iterate through the links provided with the first geocode job and extract the 'succeeded' link
$Links = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Link;
foreach ($Links as $Link) {
 if ($Link['name'] == "succeeded")
 $successUrl .= $Link;

// Access the URL for the successful requests, and convert response to an XML element
$successUrl .= "?output=xml&key=".$key;
$successResponse = file_get_contents($successUrl);
$successResponseBody = new SimpleXMLElement($successResponse);

// Loop through the geocoded results and output addresses and lat/long coordinates
foreach ($successResponseBody->GeocodeEntity as $entity) {
 echo $entity->GeocodeResponse->Address['FormattedAddress'],"<br>";
 if (!$entity->GeocodeResponse->RooftopLocation) {
 echo $entity->GeocodeResponse->InterpolatedLocation['Longitude'].", ";
 echo $entity->GeocodeResponse->InterpolatedLocation['Latitude']."<br>";
 else {
 echo $entity->GeocodeResponse->RooftopLocation['Longitude'].", ";
 echo $entity->GeocodeResponse->RooftopLocation['Latitude']."<br>";



The Output:

Job Created:
Request Status: Created
Job ID: 15702ecbc3724f3eb4a67dd48962b20e
Job Status: Pending

Checking status until complete…
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Completed

291 Broadway, New York, NY 10007-1814
-74.006134, 40.715
599 Broadway, New York, NY 10012-3235
-73.99702, 40.725111
Empire State Building, NY
-73.98581, 40.748435

Pages: [1]
Mingle Forum by cartpauj
Version: ; Page loaded in: 0.025 seconds.

28/08/2010 | Comments Closed