Support

Home Forums Add-ons User Submitted HOW-TO: Update google map latitude/longitude after importing, using address

Solving

HOW-TO: Update google map latitude/longitude after importing, using address

    • radgh

    • September 25, 2015 at 5:54 am

    I’ve recently imported a ton of store locations into a WordPress website using the plugin “WP All Import”. Many of the fields were imported into Advanced Custom Fields, including the address.

    However, neither ACF nor WP All Import calculated the latitude/longitude coordinates for these locations during the import. (and they weren’t in the spreadsheet, which would have saved a ton of time!)

    Going through 1000+ entries by hand would have taken forever. So I built a script that runs through them. It’s semi-manual, you just have to refresh the page as you step through them. It’s not an extensive plugin, but it works.

    INSTRUCTIONS

    1. Grab the file from this Gist and upload it to your theme.

    2. Configure the options in the variable $ld_recalc. You MUST set the post type, and set the different field keys (see Finding the Field Key).

    3. Call the file at the top of your functions.php script using include "recalculate-acf-locations.php";

    4. Visit your site with this query var on the end of the url, eg: http://example.com/?acf-recalc-locations

    5. You should see a results screen like this.

    6. Refresh the page again to continue scanning locations. Note: Google Maps API will prevent you from scanning too quickly.

    MORE INFORMATION

    Every time you visit the URL from Step 4, it will scan a certain amount of locations. If the lookup fails or it can’t save the results, it will abort and will scan the failed location again later.

    When a scan is successful, the lat/lng is saved to the given google_map field as well as to custom fields named “latitude” and “longitude”. A successfully scanned location is not scanned again until the “scan_identifier” changes.

  • Thanks for posting this how to, I’m sure it will come in handy for others that are trying to import map fields using All Import.

    • portegno

    • September 23, 2016 at 3:08 am

    hi radgh!

    I’m running into the same problem with Google Address importing, but I can’t use your script, it always appears this message:
    ACF Recalc Locations: All locations have latitude/longitude present! All done!

    But my 500+ records are still not updated. If I go to each record and update the record by hand it works ok, but I can’t update them using your script.

    I have updated the field for google map with my field id:
    ‘google_map’ => ‘field_56e9d8cb41509’,

    Do you have an idea of what I’m doing wrong?

    thanks for your help.

    Gabriel

    • radgh

    • September 23, 2016 at 4:22 am

    Could you check the post type? It returns “All locations updated…” when the WP Query is empty. So if it happens immediately, it must not be finding any results.

    If your records aren’t publicly queryable you might need to change post_status to include draft, private, trash, etc. I recommend debugging the WP_Query object that is generated and try changing the parameters if needed. It’s just a script that is meant to be modified so don’t worry too much about getting your hands dirty.

    • portegno

    • September 23, 2016 at 4:58 am

    It’s working ok now, it was a typo on the CPT name.

    Thanks for your help and such a great script!!

  • This worked amazing about 2 months ago wicked work guys! I added some more locations to a site yesterday and this script no longer works. Same site, same code.

    It simply goes straight to the error:
    “Google Maps returned an empty response”
    “ERROR! Google map could not locate this address. Aborting operation.”

    This is odd as when I manually enter the address into the Google Map field it finds the location perfectly.

    I am wondering if there has been a change to the Google API or an update to the Google map field in ACF which is preventing this script for working as it did 2 months ago? I hope there could be a quick fix as we have 1500 locations to GeoCode

    • radgh

    • October 21, 2016 at 9:34 am

    @ashercharles Take a look at the line that includes “file_get_contents”. There’s a URL being passed in there. Echo that out and see if that URL gives you the error. That would probably be what you want to update. I’m not sure if they have changed anything or if this is just a bug. Try another location, too, maybe?

  • Just did some further testing and on my localhost, this script works fine however when it is run from the live testing server it doesn’t work, so looks like a server config issue, not an issue with the script.

    • radgh

    • October 21, 2016 at 9:45 am

    @ashercharles You might want to try modifying the code to use wp_remote_get. If I spent the time to properly maintain this code, I would have used it instead of file_get_contents.

    https://codex.wordpress.org/Function_Reference/wp_remote_get

    This may or may not help you with the issue. But good luck!

  • Looks like the testing server doesn’t allow “allow_url_include”

  • Thanks for the documentation, all fixed, maybe commit this to the Git, as I have it all working on the staging live server now.

    Replace:
    $json = file_get_contents(“http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false”);

    With:
    $request = wp_remote_get(“http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false”);
    $json = wp_remote_retrieve_body( $request );

    • radgh

    • October 21, 2016 at 10:37 am

    Thanks @ashercharles, I updated the gist with your edits.

    • jasonSP

    • November 29, 2016 at 10:19 pm

    Hi guys,

    i’ve used this script previously and it worked fantastically well, so firstly thank you very much for this as it’s saved me a lot of time of manually updating them.

    I’ve come back to use this script again with the updates kindly pointed out by @ashercharles but I can’t seem to get it to work anymore. When I try go to the ?acf-recalc-locations it hangs for a while like it’s going to do something then hits the homepage.

    I tried going directly to the file to see what’s wrong and I get the following:

    Fatal error: Call to undefined function add_action() in /var/sites/f/fmnc.splitpixel.link/public_html/wp-content/themes/find-my-nearest-cinema/recalculate-acf-new-locations.php on line 75

    The line in question is this one:

    add_action( ‘wp’, ‘recalc_acf_locations_init’ );

    Has anybody else had this problem?.

    Any help would be greatly appreciated.

    Jason

    • radgh

    • November 30, 2016 at 5:42 am

    “Call to undefined function add_action” sounds like your script is not running through WordPress. Are you accessing your PHP file directly? You should be using a URL that loads your website normally, such as:

    http://example.org/?acf-recalc-locations

    And NOT like this:

    http://example.org/wp-content/themes/find-my-nearest-cinema/recalculate-acf-new-locations.php?acf-recalc-locations

    Also make sure the script is loaded in functions.php of your theme. add_action should be defined by the time it gets to it that way.

    • jasonSP

    • November 30, 2016 at 8:36 pm

    Hi radgh,

    Thanks for the response.

    I’ve done it in exactly the way you described, http://fmnc.splitpixel.link/?acf-recalc-new-locations.

    I’ve only renamed the recalc as I have another identical copy for another custom post type on the same theme.

    As soon as I try to run the script as you mention it just refreshes the homepage. I checked the tables within the db to see if it had updated them but it’s still not updating them. I remember the last time I used it that it had the white screen and it ran through all the locations with the all done text.

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.