Skip Navigation

[Closed] lat/long Coordinates

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 3 voices.

Last updated by Waqas 6 years, 11 months ago.

Assigned support staff: Waqas.

Author
Posts
#246421

How can I automatically generate lat/long coordinates from a custom field? I need it for my website. I'm using CRED so the people can add projects and add an address. I would like to display the project on the google map but I can't ask the users to search for lat/long coordinates.

Maybe 5 years ago it would work, but now everything is automated , responsive and modern with nice user experience. I have to find some solution, otherwise the lack of this feature will ruin my project 🙁

Kind regards,
Michael

#246627

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hi Michael,

It is possible not possible with toolset, but you can try Google map API to automatically generate lat/long coordinates from a custom field.
For example, you are using custom field "address" to store a string of address, like "USA, xx city, xxx street, #123", then you can use google API to get its lat/long coordinates.
With wordpress action "save_post", add codes in your theme/functions.php:

//* Function to retrieve & store latitude and longitude of an address field using Google Maps api
function geocode_address($post_id)
{
    $custom_fields = get_post_meta($post_id, 'wpcf-address', true);
    if(!empty($custom_fields))
    {
        $resp = wp_remote_get( "<em><u>hidden link</u></em>".urlencode($custom_fields["wpcf-map-marker-address"][0])."&sensor=false" );
        if ( 200 == $resp['response']['code'] ) {
            $body = $resp['body'];
            $data = json_decode($body);
            if($data->status=="OK"){
                $latitude = $data->results[0]->geometry->location->lat;
                $longitude = $data->results[0]->geometry->location->lng;
                update_post_meta($post_id, "wpcf-latitude", $latitude);
                update_post_meta($post_id, "wpcf-longitude", $longitude);
            }
        }
    }
}
add_action('save_post', 'geocode_address');

More help:
Maps API - Google Developers
hidden link

#247810

Hi Luoy,
thank you for your help, is it possible to create a new field that will be generated from few another fields, I mean an address field that will be created from following fields:
- country
- city
- street
- postal code

I understand I would need to create an additional hidden address field and then pass the values from above 4 fields?

#249179

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You can create custom fields on-fly by using Word Press API. Please see http://codex.wordpress.org/Function_Reference/add_post_meta for a reference.

However, you might want to create such fields when your CRED form is posted. For this you may look into CRED API at https://toolset.com/documentation/user-guides/cred-api/. Here you can find several actions to hook up with required event.

Please let me know if I can help you with anything related.

The topic ‘[Closed] lat/long Coordinates’ is closed to new replies.