maxhugen
Posts: 272
|
Posted: 08/21/2008, 7:01 PM |
|
I'm concatenating address fields in PHP to geocode addresses 'on the fly' via Google Maps API. My code works, but I'm wondering if there's a better way to do it? The fields are in a Record 'Detail', and I'm running the function below in both the BeforeInsert and BeforeUpdate events.
I'd really like to check if any of these fields have changed (if in an Update), so I don't try geocoding if there's no change to the address fields.
function geocodeAddress() {
// Geocode the address
global $Detail;
$address = "";
$address = $Detail->address_line1->GetValue();
if ($Detail->address_line2->GetValue() != "") {
if ($address != "") {
$address = $address.",";
}
$address = $address.$Detail->address_line2->GetValue();
}
if ($Detail->city->GetValue() != "") {
if ($address != "") {
$address = $address.",";
}
$address = $address.$Detail->city->GetValue();
}
if ($Detail->state->GetValue() != "") {
if ($address != "") {
$address = $address.",";
}
$address = $address.$Detail->state->GetValue();
}
if ($Detail->postal_code->GetValue() != "") {
if ($address != "") {
$address = $address.",";
}
$address = $address.$Detail->postal_code->GetValue();
}
if ($Detail->country->GetValue() != "") {
if ($address != "") {
$address = $address.",";
}
$address = $address.$Detail->country->GetValue();
}
if ($address!= "") {
$geo = gl_GeoCode($address);
if (latlongprec) {
$Detail->geo_latitude->SetValue($geo['latitude']);
$Detail->geo_longitude->SetValue($geo['longitude']);
$Detail->geo_precision->SetValue($geo['precision']);
}
}
}
Any suggestions please?
MTIA
_________________
Max
www.gardenloco.com | www.eipdna.com | www.chrisarminson.com |
|
|
wkempees
Posts: 1679
|
Posted: 08/22/2008, 2:29 PM |
|
Max, you keep us occupied.......
First glance, first suggestion:
function geocodeAddress($selected) {
// Geocode the address
global $Detail;
if($selected <>0){ // editmode, determined by passing the primary record id when calling
$modified = 0;
$db = new <yourconnectionname>;
$sql = 'select * from <tablename> where <unique_key>=' . $db->ToSQL($selected, ccsInteger);
$db->query($sql);
$db->next_record();
$db->close();
if ($Detail->address_line1->GetValue() <> $db->f('address_line1'))
$modified= 1;
if ($Detail->address_line2->GetValue() <> $db->f('address_line2'))
$modified= 2;
if ($Detail->city->GetValue() <> $db->f('city'))
$modified= 3;
if ($Detail->state->GetValue() <> $db->f('state'))
$modified= 4;
if ($Detail->postal_code->GetValue() <> $db->f('postal_code'))
$modified= 5;
if ($Detail->country->GetValue() <> $db->f('country'))
$modified= 6;
}
if ( $selected==0 || $modified !=0 ) {
$address = "";
$address = $Detail->address_line1->GetValue();
if ($Detail->address_line2->GetValue() != "") {
if ($address != "") {
$address .= ",";
}
$address .= $Detail->address_line2->GetValue();
}
if ($Detail->city->GetValue() != "") {
if ($address != "") {
$address .= ",";
}
$address .= $Detail->city->GetValue();
}
if ($Detail->state->GetValue() != "") {
if ($address != "") {
$address .= ",";
}
$address .= $Detail->state->GetValue();
}
if ($Detail->postal_code->GetValue() != "") {
if ($address != "") {
$address .=",";
}
$address .= $Detail->postal_code->GetValue();
}
if ($Detail->country->GetValue() != "") {
if ($address != "") {
$address .= ",";
}
$address .= $Detail->country->GetValue();
}
if ($address!= "") {
$geo = gl_GeoCode($address);
if (latlongprec) {
$Detail->geo_latitude->SetValue($geo['latitude']);
$Detail->geo_longitude->SetValue($geo['longitude']);
$Detail->geo_precision->SetValue($geo['precision']);
}
}
}
}
Stick the function in the page AfterInitialize Event.
Next in the Events of the Record:
BeforeInsert Event, rightclick the event, Add Action: 'Call Function'
Functionname: geocodeAddress
Parameter 1: 0
BeforeUpdate Event, rightclick the event, Add Action: 'Call Function'
Functionname: geocodeAddress
Parameter 1: CCgetFromGet('xxxxxxx_id',0)
replace 'xxxxxxx_id' with the URL parm you are using.
Within the above source code
replace <yourconnectionname>
replace <tablename> and <unique_key>
Build and tested (without the geocode).
Greetings.
Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)
if you liked this info PAYPAL me: http://donate.consultair.eu
|
|
|
maxhugen
Posts: 272
|
Posted: 08/22/2008, 8:14 PM |
|
Quote :Max, you keep us occupied.......
Sorry... new project, new ideas... but not enough skill !
Thanks, works great. Having fun playing with Google Maps API.
Cheers, Max
_________________
Max
www.gardenloco.com | www.eipdna.com | www.chrisarminson.com |
|
|
wkempees
Posts: 1679
|
Posted: 08/23/2008, 3:27 PM |
|
you're welcome.
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)
if you liked this info PAYPAL me: http://donate.consultair.eu
|
|
|
|