-1

I'm uploading a csv file and getting address field in $address variable, but when I pass $address to google maps, its showing me error,

file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=9340+Middle+River+Street%A0%2COxford%2CMS%2C38655): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request.

I searched for its solution, I found one that to encode only address but its also not working for me...

CODE

if (!empty($address)) {
        $geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address));
        $geo = json_decode($geo, true);
        if ($geo['status'] = 'OK') {
            if (!empty($geo['results'][0])) {
                $latitude = $geo['results'][0]['geometry']['location']['lat'];
                $longitude = $geo['results'][0]['geometry']['location']['lng'];
            }
            $mapdata['latitude'] = $latitude;
            $mapdata['longitude'] = $longitude;
            return $mapdata;
        } else {
            $mapdata['latitude'] = "";
            $mapdata['longitude'] = "";
            return $mapdata;
        }
    }

Error is at line

$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address));

Have I missed anything.! Any help is much appreciated.. Thanks

Aamir
  • 2,173
  • 1
  • 29
  • 58

2 Answers2

1

you need to use google api key

function getLatLong($address){
   if(!empty($address)){
    //Formatted address
    $formattedAddr = str_replace(' ','+',$address);
    //Send request and receive json data by address
    $geocodeFromAddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false'); 
    $output = json_decode($geocodeFromAddr);
    //Get latitude and longitute from json data
    $data['latitude']  = $output->results[0]->geometry->location->lat; 
    $data['longitude'] = $output->results[0]->geometry->location->lng;
    //Return latitude and longitude of the given address
    if(!empty($data)){
        return $data;
    }else{
        return false;
    }
 }else{
    return false;   
 }
}

Use getLatLong() function like the following.

$address = 'White House, Pennsylvania Avenue Northwest, Washington, DC, United States';
$latLong = getLatLong($address);
$latitude = $latLong['latitude']?$latLong['latitude']:'Not found';
$longitude = $latLong['longitude']?$latLong['longitude']:'Not found';

To specify a Google API key in your request, include it as the value of a key parameter.

$geocodeFromAddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=true_or_false&key=GoogleAPIKey');

i hope this will help you.

D Coder
  • 572
  • 4
  • 16
1

It looks like the problem is with your data set. The part of the url that is being encoded as %A0 by urlencode($address) is a special non-breaking space character, rather than a regular space.

See here for more information on the difference: Difference between "+" and "%A0" - urlencoding?

The %A0 character isn't accepted in this context, but you can do a quick str_replace() on the result of urlencode() to replace all these special space characters with the + symbols that regular spaces would have resulted in.

  $geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . str_replace('%A0', '+', urlencode($address)));
Community
  • 1
  • 1