Support

Account

Home Forums General Issues Convert ACF Address Fields into Google Maps URL

Solving

Convert ACF Address Fields into Google Maps URL

  • I’m trying to replicate the google maps link functionality found in The Events Calendar under their Venue/Location section. You enter in Address, City, State and Zip, and it returns a google maps lin (in lat/long) format.

    https://demo.theeventscalendar.com/event/standard-event-no-rsvp-or-tickets/

    It turns this:
    9081 Santa Monica Boulevard
    West Hollywood, CA 90069 United States

    Into this:
    https://www.google.com/maps/search/?api=1&query=34.0816174%2C-118.3893427

    I’ve got the address fields (Address, City, State and Zip) already built. How would I go about generating the google maps link. Could the resultant value(s) be stored back on the same post after generated?

    I know I’ll need to setup some API keys and all that as well.

    NOTE: I’m not talking about using ACF Pro Google Maps field. Though that might be next steps.

  • ACF google maps field is not a Pro feature, this field type should be available in the free version. Unfortunately I don’t know how to convert an address into map coordinates.

  • I’ve done some more searching and found this “WordPress function to convert address to Lat/Long”

    https://gist.github.com/bradp/4999343

    But I’m only enough of a programmer to realize “this could possibly be used.” But that’s the extent of my programming knowledge 🙂

  • I do not know if that will work or not, I really don’t know much about the google API.

    I can tell you that ACF stores and array in a google maps field.

    
    array(
      'address' => '',
      'lat' => '',
      'lng' => ''
    )
    

    The map is rendered in the admin using JavaScript, and again, I don’t know how this works.

    It is possible that inserting an array with the address and empty lat/lng might cause ACF to do the lookup, but without testing it I don’t know if that will work. Otherwise you would need to get the lat/lng before updating the value.

    Outside of both the fields you are already using plus an additional map field I really don’t have any suggestions for you. If you don’t want to use a ACF Gmap field then you’ll need to figure out how to use the google API, which is beyond what I can help you with. I depend on existing plugins to deal with google maps mostly because google documentation is probably the most obtuse documentation I’ve ever read, although the WP guberbug documentations is moving up the charts.

  • It works with Desktop/Mobile.

    <?php 
    $location = get_field('location');
    if( $location ) {
    
        // Loop over segments and construct HTML.
        $address = '';
        foreach( array('street_number', 'street_name', 'city', 'state', 'post_code', 'country') as $i => $k ) {
            if( isset( $location[ $k ] ) ) {
                $address .= sprintf( '<span class="segment-%s">%s</span>, ', $k, $location[ $k ] );
            }
        }
    
        // Trim trailing comma.
        $address = trim( $address, ', ' );
    
        // Display HTML.
        echo '<p>' . $address . '. </p>';
    }
    ?>
    
    <a href="https://www.google.com/maps/place/<?php echo str_replace(' ','+',$location['address']); ?>" target="_blank"> Map adress link </a>
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.