This topic is locked
[SOLVED]

 Script to move lat & lang in your Table

8/8/2018 12:51:42 PM
PHPRunner General questions
S
shoppy author

Let's take this topic a step higher.
I came across a script gives you the latitude and longitude values on an address



<?php

// Fixed addresses from the example, they work!

//$address = "1600 Pennsylvania Ave NW Washington DC 20500";

//$address = str_replace(" ", "+", $address);

//$region = "USA";
$address = $values['Adres'];

$city = $value['Woonplaats'];

$combinedAddress = $address . ',' . $city;

$region = "Europe";
$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;


This works.
Now I like to take the values from 'Address' and 'City' from my Table copied into this script and then the value of $lat en $log should be added to the fields 'Latitude' and 'Longitude'. when a new client is added to my database.
I think this needs to be done in 'Before record added' in the Add Page.

admin 8/8/2018

If you add this code to event like BeforeAdd then adding the following to end of your code should do the job:

$values["lat"]=$lat;

$values["lng"]=$long;
S
shoppy author 8/9/2018



If you add this code to event like BeforeAdd then adding the following to end of your code should do the job:

$values["lat"]=$lat;

$values["lng"]=$long;



Thanks but that gives an error:
<<< Record not added. >>>
Unknown column 'lat' in 'field list'
When I change the part in:

$values["Latitude"]=$lat;

$values["Longitude"]=$long;


it gives no errors but does not add anything to the two fields 'Latitude' and 'Longitude'

admin 8/10/2018

You need to make sure that variables like $lat and $long do in fact contain correct values and those values are compatible with the data type of Lattitude and Longitude fields.

S
shoppy author 8/11/2018

Thanks Sergey, that made it happen.
I had to give the two fields "Latitude" and "Longitude" 7 decimal digits as a number (in the Editor tab).
Then the right code for "Before record added" on the Add and Edit Event is:

// Get the variables

$address = $values['Adres'];

$city = $values['Woonplaats'];

$combinedAddress = $address . ',' . $city;

$address = str_replace(" ", "+", $combinedAddress);

$region = "Europe";
// Get the Geocode from GoogleMaps

$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'};
// Values $lat en $long are being placed in the fielfds "Latitude" and "Longitude"

$values["Latitude"]=$lat;

$values["Longitude"]=$long;
// Now the record is added or edited
return true;