0

I am using Bing Maps API to Geocode addresses. I took out an example from this page https://msdn.microsoft.com/en-us/library/ff817004.aspx which was form based process, in which you have to submit the address to get longitude and latitude. Now the requirement in front of me is that, I would like to manually put addresses in PHP page and get longitude and latitude. It is not happening. This is what we have tried as of now:

   $key=key;

   $baseURL = "http://dev.virtualearth.net/REST/v1/Locations";

   $query ="Rajalakshmi Mill Road, Singanallur";
   $findURL = $baseURL."/".$query."?output=xml&key=Aukd2ilaNJexSjdSjdkoGr26cpoqaVUhOg0MbDTZtfPGClozardCt_1iRscSm5Xo";

   $output = file_get_contents($findURL);
   $response = new SimpleXMLElement($output);

   // Extract and pring latitude and longitude coordinates from results
   $latitude = $response->ResourceSets->ResourceSet->Resources->Location->Point->Latitude;
   $longitude = $response->ResourceSets->ResourceSet->Resources->Location->Point->Longitude;
   $longitude = $response->ResourceSets->ResourceSet->Resources->Location->Point->Longitude;
   $address = $response->ResourceSets->ResourceSet->Resources->Location->Address->FormattedAddress->State;

   echo "Latitude: ".$latitude."<br>";
   echo "Longitude: ".$longitude."<br>";
   echo $address1;

What shall I do?

Ahmed Numaan
  • 1,034
  • 1
  • 10
  • 26
Husain
  • 119
  • 2
  • 8

2 Answers2

2

Looking at the MSDN page, I see that you have to do this before sending in the query,

$query = str_ireplace(" ","%20",$_POST['query']);

The api requires you to manually replace the space character with %20. You could also use url_encode as a safer option.

Other options include using curl as @Domenik Reitzner suggested.

$ch = curl_init();

// Set query data here with the URL
curl_setopt($ch, CURLOPT_URL, $findURL); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$content = trim(curl_exec($ch));
curl_close($ch);
$response = new SimpleXMLElement($content);

Update:

I just saw the update to your answer and you included a URL in it, when I tried to access it this was the result.

XML

By the way, I recommend you delete your credentials and generate a new one and also remember not to post your API keys online next time.

Update 2

Found the problem, the baseUrl should be

http://dev.virtualearth.net/REST/v1/Locations

instead of

http://dev.virtualearth.net/REST/v1/Routes

The code in your question has the right URL but the error you got has the wrong URL.

Error in code

This is the result I get when I query http://dev.virtualearth.net/REST/v1/Locations/Rajalakshmi%20Mill%20Road,%20Singanallur,%20Coimbatore?output=xml&key=YOURAPIKEY

response

kks21199
  • 1,116
  • 2
  • 10
  • 29
0

This is what I get as a response with the provided link:

<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
    <Copyright>
        Copyright © 2018 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.
    </Copyright>
    <BrandLogoUri>
        http://dev.virtualearth.net/Branding/logo_powered_by.png
    </BrandLogoUri>
    <StatusCode>400</StatusCode>
    <StatusDescription>Bad Request</StatusDescription>
    <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
    <ErrorDetails>
        <string>One or more parameters are not valid.</string>
        <string>
            travelMode: This parameter value has an invalid format.
        </string>
    </ErrorDetails>
    <TraceId>
        dea21427b82347128fc5f7012bda5592|DB40060630|7.7.0.0
    </TraceId>
    <ResourceSets/>
</Response>

Which brings me back to the expected URL format:

http://dev.virtualearth.net/REST/v1/Locations/countryRegion/adminDistrict/postalCode/locality/addressLine?key=yourBingMapsKey
Domenik Reitzner
  • 1,583
  • 1
  • 12
  • 21