Hello,
I’m feeding TEC events and venues into my native Android app with WP REST API, however I still have not been able to get the venue lat/lng data as an endpoint.
In otherwords: In PHP, given just venue ID, how can I get venue latitude and longitude?
SOLVED: As I wrote this post, came up with a solution:
My query: http://www.mywebsite.com/wp-json/wp/v2/tribe_venue?per_page=100&fields=id,title.rendered,city,address,latlng&order=asc
Endpoint code from my theme’s functions.php (code below is just for the lat/lng part of my query):
add_action( 'rest_api_init', 'slug_register_venue_latlng' );
function slug_register_venue_latlng() {
register_rest_field( 'tribe_venue','latlng',
array(
'get_callback' => 'slug_get_venue_latlng',
'schema' => null
)
);
}
function slug_get_venue_latlng( $object, $field_name, $request ) {
$postId = $object[ 'id' ];
// Check if lat/lng exists for the venue post, otherwise, use zero.
if ( class_exists( 'Tribe__Events__Pro__Geo_Loc' ) ) {
$output[ 'lat' ] = (float) get_post_meta( $postId, Tribe__Events__Pro__Geo_Loc::LAT, true );
$output[ 'lng' ] = (float) get_post_meta( $postId, Tribe__Events__Pro__Geo_Loc::LNG, true );
} else {
$output = array(
'lat' => 0,
'lng' => 0
);
}
return $output;
}