Reputation: 19425
I'm having problems using geocoder and returning correct lat / lang coordinates.
The controller
address is returning correct geo location, but when I'm using the character ø
in the address, I'm gettin som weird coordinates.
Here is my code:
$controller = "Skovveien+7+0257+oslo+norway";
// base = Strømmen Storsenter, Støperiveien 5, 2010 Strømmen, Norway
$t1 = htmlentities("Strømmen+Storsenter+2010+strømmen+norway", ENT_QUOTES, 'UTF-8');
$t2 = htmlentities("Støperiveien+5+2010+strømmen+norway", ENT_QUOTES, 'UTF-8');
$adr = htmlentities($controller, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Controll<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
$adr = htmlentities($t1, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Shopping mall<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
$adr = htmlentities($t2, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Adresse + post code<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
public function getGeoLocation($adr) {
// Decode special chars
$adr = html_entity_decode($adr, ENT_QUOTES, 'UTF-8');
$url = "http://maps.google.com/maps/api/geocode/json?address=$adr&sensor=false";
$jsonData = file_get_contents($url);
$geocode = json_decode($jsonData);
if($geocode->status == 'OK') {
$geocode = $geocode->results[0];
$res['lat'] = $geocode->geometry->location->lat;
$res['lng'] = $geocode->geometry->location->lng;
return $res;
} else {
return false;
}
}
I know it seems a bit pointless first using htmlentities
, then using html_entity_decode
in the function. But if I don't, Google returns ZERO_RESULT
.
Can anyone help me with correct code for fetching lat/lang address for street names with international characters?
Upvotes: 0
Views: 1942
Reputation: 449435
If your PHP file is UTF-8 encoded, this condensed example should work:
$adr = urlencode("Strømmen Storsenter, Støperiveien 5, 2010 Strømmen, Norway");
$url = "http://maps.google.com/maps/api/geocode/json?address=$adr&sensor=false";
$jsonData = file_get_contents($url);
Edit: Weirdly, I get an APPROXIMATE
address when I use
Strømmen Storsenter, Støperiveien 5, 2010 Strømmen, Norway
but a ROOFTOP
one when I use only the street address:
Støperiveien 5, 2010 Strømmen, Norway
Even though, as you can see on the map, Google is perfectly aware that a location named "Strømmen Storsenter" exits at that particular address!
This doesn't look like a character set issue after all, but a weirdness in the way how Google parses requests with place names in it.
Upvotes: 1