Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP - Get Geocode (lat, long) from Address
<?php
$address = 'avenida+gustavo+paiva,maceio,alagoas,brasil';
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
?>
@sajaddp

This comment has been minimized.

Copy link

commented Jul 22, 2017

Hello
It is a good code.
Is there a way to get coordination of several countries at the same time?

@amitm13

This comment has been minimized.

Copy link

commented Oct 6, 2017

nice thanks you

@Angelonyman

This comment has been minimized.

Copy link

commented Oct 17, 2017

thanks, it is really simple and generate the code I need...

aqui e brasil :)

@mycarrysun

This comment has been minimized.

Copy link

commented Dec 27, 2017

If anyone looking for the timezone for an address (that's how I found this gist) I created another gist https://gist.github.com/mycarrysun/e4dfe947dc8ce7421bb2fd81eeed0093

@jharriot

This comment has been minimized.

Copy link

commented Apr 21, 2018

UPDATED;

`<?php

$address = "1600 Pennsylvania Ave NW Washington DC 20500";
$address = str_replace(" ", "+", $address);
$region = "USA";

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
echo $lat."
".$long;

?> `

@samanshahzadi

This comment has been minimized.

Copy link

commented May 31, 2018

Hello

I am getting this message when I am applying the above function

{
"error_message" : "You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_",
"results" : [],
"status" : "OVER_QUERY_LIMIT"
}

Can someone help me?

Thanks

@Burshyn

This comment has been minimized.

Copy link

commented Aug 15, 2018

You have exceeded your daily request quota for this API!
We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_"

@djtetsu

This comment has been minimized.

Copy link

commented Oct 25, 2018

STRONGLY SUGGEST AGAINST GOOGLE MAPS!

They jacked up prices ridiculously and this will be a waste of time. Google "Google Maps Price hike". Our monthly fee went from negligible to over $700.

We are looking at developer dot mapquest dot com for a solution

@mpcleverdon

This comment has been minimized.

Copy link

commented Nov 1, 2018

check out open street map and/or leaflet it has everything you need

@mhmtcbn

This comment has been minimized.

Copy link

commented Aug 7, 2019

UPDATED;

`<?php

$address = "1600 Pennsylvania Ave NW Washington DC 20500";
$address = str_replace(" ", "+", $address);
$region = "USA";

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
echo $lat."
".$long;

?> `

Hello, I want to get the postcode, I was able to get the postcode using the code below. The order of the postal code changes when the street address changes. Another information comes in place of the postal code.

$postalcode = $json->{'results'}[0]->{'address_components'}[6]->{'short_name'};

I couldn't solve the problem. {'address_components'}[6] Number is constantly changing 6, 5, 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.