How to get Latitude and Longitude to build a heatmap

Hi. i am following this tutorial to build a heatmap based on city location.

the only thing i am missing is how to retrieve the latitude and longitude data. In the tutorial in the table there are already the latitude and longitude data (from minute18:20 he starts to use lat and long data that are already in the db).

i would like to know if there is some way when i specify the city and insert the record, to retrieve (via API i guess) latitude and longitude data in order to write them in the db.

i remember some some months ago a tutorial were that part was not missing but i can not recall where it is.

Thanks

1 Like

Hi Mhanu
I get it using the following google geocode API

sc_warning=β€˜off’;
$addressData = ({Address1}.’, β€˜.{Address2}.’, β€˜.{Address3}.’, '.{Address4});

$geocode = file_get_contents(β€œhttps://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($addressData)."&key=[YOUR KEY]”);

$output = json_decode($geocode);

$lat=$output->results[0]->geometry->location->lat;
$long=$output->results[0]->geometry->location->lng;

{longitude}=$long;
{latitude}=$lat;
sc_warning=β€˜on’;

Regards
Doug Maclean

2 Likes

First things first. Thank you for taking time to answer.

i have put my code in the onvalidate event of the form.

sc_warning=β€˜off’;
$addressData = {city};

$geocode = file_get_contents(β€œhttps://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($addressData)."&key=[MY KEY]”);

$output = json_decode($geocode);

$lat=$output->results[0]->geometry->location->lat;
$long=$output->results[0]->geometry->location->lng;

{longitude}=$long;
{latitude}=$lat;
sc_warning=β€˜on’;

do the square brackets need to be there?
i get an error on that line of code

Thank you again

1 Like

No, the square brackets do not need to be there just your key.

1 Like