Geo Call

By nmsinc - November 2, 2013

I need to call the geo plugin into action just after the sql update below - how is this done? - Thanks nmsinc

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']));
}

nmsinc

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