Skip Navigation

[Resolved] How to get Lat and Long of Address field

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

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 1 reply, has 1 voice.

Last updated by smileBeda 3 years, 9 months ago.

Author
Posts
#1934815

I am trying to get Lat and Long of address for a post programmatically.

Note, Toolset does not anymore store an option with lat and long mapped to the actual human readable address, and it does not store a postmeta with lat and long, unless you pass lat and long in the actual address field (where usually we will have human readable address)

Instead, it uses a custom database table, which in turn again does not always store lat and long but a mix of human readable address and lat and long or human address and human address, depending on wether the address exists or not, and a geometry column that (probably) stores the proper lat and long, but it is a real mess when it comes to get that data with efficient code, because we never know what we get back from the database, there can be at least 4 different type of result:
- human address stored in post meta
- lat and long in post meta
- human address mapped to lat and long in custom database table
- human address mapped to another human address in custom database table

Bottom line, how do I get a lat and long of Toolset address field programmatically by post id, independent from wether the address existing in google or not?

Do I need to create a code that saves lat and long into an actual proper postmeta entry when saving the post, or is there an API I am not aware of?
(I googled and couldn't really find any doc in regard)

I need lat and long to create an index for WP Grid Builder, so it has to be something that I can get lat and long by Post ID, independently from wether the address is a lat and long, or existing human readable address, using efficient and native code, not Custom SQL Queries and geometry conversion if possible.

Can you give me more information on this?

Thanks

#1935069

Nevermind, this is the solution, in case anyone else is looking for it (I sees numerous Support tickets leading quite into other, wrong directions, so probably this will help users)

$object_id = //Define the Post ID however you need/want/can
types_render_field("custom-address-field-slug", array('format'=>'FIELD_LATITUDE', 'item'=>$object_id));
types_render_field("custom-address-field-slug", array('format'=>'FIELD_LONGITUDE', 'item'=>$object_id));

Above code will get the lat and long of the custom address field by post id.

In a WP Grid Builder Index code, this could look like this:

function filter_maps_index( $row, $object_id, $facet ) {

	if($facet['id'] == 5 ){//change facet ID

		$coordinates['latitude'] = types_render_field("custom-address-field-slug", array('format'=>'FIELD_LATITUDE', 'item'=>$object_id));

		$coordinates['longitude'] = types_render_field("custom-address-field-slug", array('format'=>'FIELD_LONGITUDE', 'item'=>$object_id));
	
		if ( isset( $coordinates['latitude'], $coordinates['longitude'] ) ) {

			$row['facet_value'] = $coordinates['latitude'];

			$row['facet_name'] = $coordinates['longitude'];

		}

	}

	return $row;

}

add_filter('wp_grid_builder/indexer/row','filter_maps_index',10,3);

Cheers.