Encoding variables for Google Maps geocoder with PHP

1k Views Asked by At

I am trying to encode variables for the URL for the google maps geocoding API with PHP. I have used both urlencode and rawurlencode and I get the same result for

Székesfehérvár, Hungary

PHP returns

Sz%E9kesfeh%E9rv%E1r%2C%20Hungary

Which cannot be decoded by any of the online decoders.

When I encode with the online encoder tools I get:

Sz%C3%A9kesfeh%C3%A9rv%C3%A1r%2C+Hungary

When I plug that in directly to the Google Maps geocoder URL, it is successful. The PHP function fails every time.

This is the code I am using:

function getLatandLong($landmark,$city,$state)
{

global $lat;
global $long;
global $city;
if (!empty($landmark)) {  $encoded_vars = "$landmark,$city,+$state";
} else { $encoded_vars = "$city, $state"; }

$encodedVars = urlencode($encoded_vars);
$doc = new DOMDocument();

$doc->load("http://maps.google.com/maps/api/geocode/xml?address=$encodedVars&sensor=false"); //input address
1

There are 1 best solutions below

0
On BEST ANSWER

Well, I tried

$encodedVars = utf8_encode($encoded_vars);

And that seems to be working, even though the echo shows (probably because I don;t have the right page encoding set on my catch page:

Székesfehérvár,+Hungary