Support

Account

Home Forums Front-end Issues Google map address

Solving

Google map address

  • How to render Google map text address only. When I use command below …

    <?php the_field(‘location’);

    I get text address and co-ordinates as follows …
    Location: Digiplex- Poway, Poway Road, Poway, CA, United States, 32.954766, -117.03976399999999

    Now How to Render as follows ….??? ( No Co-ocrdinates and text as address format)

    Location:
    Digiplex- Poway
    Poway Road
    Poway, CA

  • You could try doing php explode

    
    $address = explode( ',', get_field(โ€˜locationโ€™) );
    
    echo $address[0].'<br />';
    echo $address[1].'<br />';
    echo $address[3].'<br />';
    echo $address[4].'<br />';
    echo $address[5].'<br />';
    
    
  • The above was close… I’m crap with terminology so bear with me. The above doesn’t work because it won’t return the right stuff (see, crap with terminology ๐Ÿ˜‰ )

    You have to create a variable for the location field so you can access the address array.

    $location = get_field(‘location’)
    $address = explode( “,” , $location[‘address’]);
    echo $address[0].'<br/>’; //place name
    echo $address[1].'<br/>’; // street address
    echo $address[2].’,’.$address[3]; // city, state zip

    This would display as (using your address example):
    Digiplex- Poway
    Poway Road
    Poway, CA

    I would just hard code the word “Location:” above this if you need it at all.

    If you do: print_r(explode( “,” , $location[‘address’]));

    You will get the array of information that is available from the address object, in case you want country or don’t want street address, etc. Obviously you can tweak the html to your needs when you echo out each thing.

    Hope this was helpful!

    @kingafrojoe definitely got me on the right path, so thanks dude!

  • Hi there, okadots. I have run into the same dilemma: trying to display the address all in one line without the coordinates at the end.

    I am new to PHP, so I’m struggling to get your code to work. Can you paste the complete code that you used that worked? I would really appreciate it!

    Thanks so much ๐Ÿ™‚
    Raeanne

  • hey raeanne,

    sure – here you go. i am using the ACF repeater and the HTML address tag (i’m making a page with multiple contacts listed)

    <?php if( have_rows(‘contacts’) ): ?>
    <section class=”contact_list”>
    <?php while( have_rows(‘contacts’) ): the_row();

    // vars
    $contact_address = get_sub_field(‘contact_address’);

    ?>
    <address>
    <?php $address = explode( “,” , $contact_address[‘address’]);
    echo $address[1].'<br/>’; //street number and line break
    echo $address[2].’,’.$address[3]; //city, state
    ?>
    </address>
    <?php endwhile; ?>
    </section>
    <?php endif; ?>

  • if you do:
    echo $address[0] — you would get the name of the place
    echo $address[4] — should be the zip code

    you can just put in different values to see what each one is, or you can do print_r($address); which will give you the array with all of the values.

  • Thank you so much! It took a little finagling, but I figured it out. I’m using the Options page add-on, and calling the company address into the footer. Here is the final code that did the trick:

    <?php 
                                    
    // vars
    $contact_address = get_field('company_address', 'option');
    
    ?>
    <address class="footer-address street-address">
    <?php $address = explode( "," , $contact_address['address']);
    echo $address[0].', '; //street number
    echo $address[1].','.$address[2]; //city, state + zip
    ?>
    </address>

    I really appreciate the help! ๐Ÿ™‚

  • I haven’t always found this to be accurate. If you search by place, the first object in the array is going to be the place name. But, if you search by numbered address, the first object is going to be the address number instead.

    Some “places” aren’t going to start with a name if they aren’t registered with Google, for example. Instead, they are going to start with a number address.

  • This is the solution I ended with:

    <?php $location = get_field('google_map');
    if( $location ): ?>
        <?php echo esc_attr($location['street_name']); ?>&nbsp;
    	<?php echo esc_attr($location['street_number']); ?><br />
    	<?php echo esc_attr($location['post_code']); ?>&nbsp;
    	<?php echo esc_attr($location['city']); ?><br />
    	<?php echo esc_attr($location['country']); ?>
    <?php endif; ?>

    No state in Belgium but could add
    <?php echo esc_attr($location['state']); ?>
    as well

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

You must be logged in to reply to this topic.