Geo Call

By Dave - November 4, 2013

Hi nmsinc, 

There's no real build-in code to do that.  The automatic features of the GeoCoder set fake values in $_REQUEST['latitude'] etc so they get saved automatically.  Your best bet is to copy some of the code from _geocoder_form_geocodeTable().

Here's some (untested) code to get you started: 

// get lat/lng
$address = "{$Record['address']}, {$Record['city']}, {$Record['state']} {$Record['zip']}";
list($lat, $lng) = geocodeAddress($address);
$geocode_error = geocode_error();
if ($geocode_error && $geocode_error != 'NO_ADDRESS' && $geocode_error != 'STATUS_ZERO_RESULTS') { die("Geocode error: $geocode_error"); }
if (!$lat || !$lng) { die("Couldn't geocode address! $address<br/>\n"); }    

And if your queries are all updating the same table (wasn't sure about csubmission) you can do it in one line like this:

  mysql_update('submission',$Record['num'],null,array('address'=>$Record['address'], 'city'=>$Record['city'], 'state'=>$Record['state'], 'zip'=>$Record['zip']));

Or my favorite: 

$colsToValues = array('address'=>$Record['address'], 'city'=>$Record['city'], 'state'=>$Record['state'], 'zip'=>$Record['zip']);
mysql_update('submission',$Record['num'],null,$colsToValues);

Hope that helps, let me know any questions.

Dave Edis - Senior Developer
interactivetools.com

By nmsinc - November 4, 2013

Hi Dave,

Sorry, I cannot see how your examples will create and store the geo latitude and longitude fields within the saved file (similar to when you save a file in CMS that posts the geo codes)

Thanks - nmsinc

nmsinc

By Dave - November 4, 2013

Hi nmsinc,

You can save them back with a mysql_update.  

Instead of: 

if( $Record['same_as_address'] )  {
  mysql_update('submission',$Record['num'],null,array('address'=>$Record['address']));
  mysql_update('submission',$Record['num'],null,array('city'=>$Record['city']));
  mysql_update('csubmission',$Record['num'],null,array('state'=>$Record['state']));
  mysql_update('submission',$Record['num'],null,array('zip'=>$Record['zip']));
}

You could have something like this:

if( $Record['same_as_address'] )  {
  // get lat/lng
  $address = "{$Record['address']}, {$Record['city']}, {$Record['state']} {$Record['zip']}";
  list($lat, $lng) = geocodeAddress($address);
  $geocode_error = geocode_error();
  if ($geocode_error && $geocode_error != 'NO_ADDRESS' && $geocode_error != 'STATUS_ZERO_RESULTS') { die("Geocode error: $geocode_error"); }
  if (!$lat || !$lng) { die("Couldn't geocode address! $address<br/>\n"); }    


  // save record
  $colsToValues = array('address'=>$Record['address'], 'city'=>$Record['city'], 'state'=>$Record['state'], 'zip'=>$Record['zip'], 'latitude' => $lat, 'longitude' => $lng);
  mysql_update('submission',$Record['num'],null,$colsToValues);
}

Note that I'm not familiar with your custom code, but you just need to save the returned values back to the longitude/latitude fields in the database.

Let me know if that works for you.

Dave Edis - Senior Developer
interactivetools.com

By nmsinc - November 4, 2013

Hi Dave,

I'm getting the following error listed below on this line of code:    $geocode_error = geocode_error();

"Fatal error: Call to undefined function geocode_error()"

Any ideas?

Thanks - nmsinc

nmsinc

By Dave - November 7, 2013

Hi nmsinc,

Apologies for the delay.  I realized I gave you code based on the geocoder 1.02 version that we hadn't released yet.  You can just remove those two error checking lines and it should work with 1.00:

if( $Record['same_as_address'] )  {
   // get lat/lng

  $address = "{$Record['address']}, {$Record['city']}, {$Record['state']} {$Record['zip']}";
  list($lat, $lng) = geocodeAddress($address);
  if (!$lat || !$lng) { die("Couldn't geocode address! $address<br/>\n"); }    

  // save record
  $colsToValues = array('address'=>$Record['address'], 'city'=>$Record['city'], 'state'=>$Record['state'], 'zip'=>$Record['zip'], 'latitude' => $lat, 'longitude' => $lng);
  mysql_update('submission',$Record['num'],null,$colsToValues);
}

Let me know if that works better.  In the meantime, I'll release our latest 1.02 code as well.

Thanks!

Dave Edis - Senior Developer
interactivetools.com

By nmsinc - November 7, 2013

HI Dave,

I would like to update to the new version - are there any specific changes that I need to know before I update as I have many pages that use the Geo plugin?

Thanks - nmsinc

nmsinc

By Dave - November 7, 2013

Hi nmsinc, 

It should be a drop-in replacement, but if you have a problem you can just revert back to the older version.  You can download 1.02 here:
http://www.interactivetools.com/add-ons/my_purchases.php

And here's the changelog: 

*** November 7, 2013 - Version 1.02 Released

NEW FEATURES
- Added geocoder_getDistanceDuration() function for getting distance and driving duration between two points
- Simplified getRecords() code (see sample_search.php for example)

CODE CHANGES & BUG FIXES
- Improved error reporting when geocoding fails for one or more reaons
- Added _distanceIsNull column alias to support older MySQL versions (http://bugs.mysql.com/bug.php?id=11694)
- Misc Code and other minor improvements

Part of the new improved error reporting is that missing function and having the plugin tell you when you've exceeded Google Map's daily limits, etc rather than just failing at geocoding the address.

Dave Edis - Senior Developer
interactivetools.com

By nmsinc - November 9, 2013

Hi Dave,

Thanks for the help - the code worked great. I now need to Geo the address, city, state and zip in a upload plugin you coded for me last year - see Plugin attached!

nmsinc
Attachments:

importclaimsubmission.php 10K

By Dave - November 11, 2013

HI nmsinc, 

I'm not familiar with that plugin and it doesn't seem to do anything on it's own (with just that file).  

If you want to geocode an entire table after importing you can do that from the plugin menu.  Or if you want it automatic you basically need to load a record after you insert it, pass it's address to geocoder, then update the record with the longitude and latitude.  

So, assuming that last mysql_insert in your plugin is the one adding a record, the code might be something like this (new code in red):

// try to geocode address
$address = "{$insertArray['field_insured_address']}, {$insertArray['field_insured_city']}, {$insertArray['field_insured_state']} {$insertArray['field_insured_zip']}";
list($insertArray['latitude'], $insertArray['longitude']) = geocodeAddress($address);

// insert record
$recordNum = mysql_insert('claims_submission',$insertArray);
saveUploadFromFilepath('claims_submission','documents',$recordNum,null,$uploadDir . $upload['filePath']);

But you'll want to do a lot of testing, and the code assumes you wan to geocode the field_insured_* fields and not field_claimant_1_* and that you want to store the results in latitude and longitude (and that those fields as well as the previous ones exist in both the database table and the $insertArray).

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com