Nilesh Patel
Nilesh Patel

Reputation: 31

Not getting correct latitude & longitude with full address

$fulladdress = 'South Bay Hospital 4016 Sun City Center Blvd Sun City Center Florida 33573';
$address = str_replace(' ','+',$fulladdress);

$url = "http://maps.googleapis.com/maps/api/geocode/json?address='".@urlencode($address)."'&sensor=false";

$result_string = @file_get_contents($url);<br>
$result = @json_decode($result_string, true);<br>
$result1[]=@$result['results'][0];<br>
$result2[]=$result1[0]['geometry'];<br>
$result3[]=$result2[0]['location'];<br>
return $result3[0]; 

I am using this Google Geocode API for getting latitude & longitude.I have passed full address "South Bay Hospital 4016 Sun City Center Blvd Sun City Center Florida 33573" but it is not giving full address latitude & longitude. When i have checked print_r($result). so it is removing "South Bay Hospital" automatically and giving latitude & longitude for "4016 Sun City Center Blvd Sun City Center Florida 33573" that's why google map marker is not coming right place.

Upvotes: 3

Views: 261

Answers (2)

Abrar Khan
Abrar Khan

Reputation: 175

Its Work for me

$address = "Your Address";
        $prepAddr = str_replace(' ', '+', $address);
        $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=' . $prepAddr . '&sensor=false');
        $output = json_decode($geocode);
        $latitude = $output->results[0]->geometry->location->lat;
        $longitude = $output->results[0]->geometry->location->lng;

Upvotes: 1

Happy Coding
Happy Coding

Reputation: 2525

Check this :

// We get the JSON results from this request
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
// We convert the JSON to an array
$geo = json_decode($geo, true);
// If everything is cool
if ($geo['status'] = 'OK') {
  // We set our values
  $latitude = $geo['results'][0]['geometry']['location']['lat'];
  $longitude =  $geo['results'][0]['geometry']['location']['lng'];
}

Upvotes: 1

Related Questions