Skip Navigation

[Resolved] Is it possible add google address coordinates in json?

This thread is resolved. Here is a description of the problem and solution.

Problem:

Is it possible add custom address field coordinates into REST API result in JSON format?

Solution:

Please try the function types_render_field(), for example:

https://toolset.com/forums/topic/is-it-possible-add-google-address-coordinates-in-json/#post-1845587

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/functions/#address

This support ticket is created 4 years 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by avansisI-2 3 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1836013

Oh! great!

I cant see in code, sorry. Relation this I have other question.... Is it possible add
google address coordinates in json?

Thanks for your help.

#1836017

Hello,

I assume you are going to output custom address field coordinates value with PHP codes.

If it is, please try the function types_render_field(), see our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/#address

#1837525
Captura de pantalla 2020-11-07 a las 19.21.17.png
Captura de pantalla 2020-11-07 a las 19.21.06.png

Yes, i'm using toolset map,

Attach a screenshoot about this,

I don't want the latitude and longitude to be displayed in the front-end, but I need to be able to pass that information in my API-rest.

Thanks for your help

#1838281

It needs custom codes, as I mentioned above, you can use Types function types_render_field() to render the custom address latitude and longitude values into your REST API hooks, for example:

add_action( 'rest_api_init', 'create_api_posts_meta_field' );
 
function create_api_posts_meta_field() {
	
	$post_type_slug = 'sample-location-a'; //replace sample-location-a with your post type slug
	$field_slug = 'direccion'; //replace direccion with your custom address field slug
	
    register_rest_field( $post_type_slug, $field_slug . '-latitude', array(  
           'get_callback'    => 'get_address_latitude',
           'schema'          => null,
        )
    );
    register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
           'get_callback'    => 'get_address_longitude',
           'schema'          => null,
        )
    );
}
 
function get_address_latitude( $object ) {
	$field_slug = 'direccion'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
	$res = types_render_field('direccion', array(
		'post_id'=> $post_id,
		'format' => 'FIELD_LATITUDE',
	));
    return $res;
}
 
function get_address_longitude( $object ) {
	$field_slug = 'direccion'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
	$res = types_render_field($field_slug, array(
		'post_id'=> $post_id,
		'format' => 'FIELD_LONGITUDE',
	));
    return $res;
}

Please replace sample-location-a with yourpost type slug, replace direccion with your custom address field slug

More help:
https://developer.wordpress.org/rest-api/

#1844457

I generate this code but it does not work properly

add_action( 'rest_api_init', 'create_api_posts_meta_field' );
  
function create_api_posts_meta_field() {
     
    $post_type_slug = 'comercio'; //replace sample-location-a with your post type slug
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
     
    register_rest_field( $post_type_slug, $field_slug . '-latitude', array(  
           'get_callback'    => 'get_address_latitude',
           'schema'          => null,
        )
    );
    register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
           'get_callback'    => 'get_address_longitude',
           'schema'          => null,
        )
    );
}
  
function get_address_latitude( $object ) {
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    $res = types_render_field('direccion', array(
        'post_id'=> $post_id,
        'format' => 'FIELD_LATITUDE',
    ));
    return $res;
}
  
function get_address_longitude( $object ) {
    $field_slug = 'direccion'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    $res = types_render_field($field_slug, array(
        'post_id'=> $post_id,
        'format' => 'FIELD_LONGITUDE',
    ));
    return $res;
}

Could you help me?

#1844883

I changed the code but it still gives an error

This is the error:

Your PHP code changes were rolled back due to an error on line 3 of file /functions.php. Please fix and try saving again.

syntax error, unexpected 'function' (T_FUNCTION)

//AÑADIR LATITUD Y LONGITUD

add_action( 'rest_api_init', 'create_api_posts_meta_field' );
  
function create_api_posts_meta_field () {
     
    $post_type_slug = 'comercio'; //replace sample-location-a with your post type slug
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
     
    register_rest_field( $post_type_slug, $field_slug . '-latitude', array(  
           'get_callback'    => 'get_address_latitude',
           'schema'          => null,
        )
    );
    register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
           'get_callback'    => 'get_address_longitude',
           'schema'          => null,
        )
    );
}
  
function get_address_latitude( $object ) {
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    $res = types_render_field('direccion', array(
        'post_id'=> $post_id,
        'format' => 'FIELD_LATITUDE',
    ));
    return $res;
}
  
function get_address_longitude( $object ) {
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    $res = types_render_field($field_slug, array(
        'post_id'=> $post_id,
        'format' => 'FIELD_LONGITUDE',
    ));
    return $res;
}
#1845341

Hello,

Since it is a custom codes problem, please provide a test site with the same problem, fill below private message box with login details and FTP access, also point out the problem REST API URL, and where I can edit your custom PHP codes, I need to test and debug it in a live website, thanks

#1845567

Thanks for the details, I can login your website, please point out the problem REST API URL, where I can see the result in front-end? thanks

#1845571

This is the api-rest file

/wp-json/wp/v2/comercios

"wpcf-direccion_comercio": "Calle Arturo Soria, 55, Madrid, España",

/formulario-de-ficha-de-comercio/ (to create a form)

#1845587

I have done below modifications in your website, add below codes in your theme file "functions.php":

add_action( 'rest_api_init', 'create_api_address_field' );
  
function create_api_address_field() {
     
    $post_type_slug = 'comercios'; //replace sample-location-a with your post type slug
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
     
    register_rest_field( $post_type_slug, $field_slug . '-latitude', array(  
           'get_callback'    => 'get_address_latitude',
           'schema'          => null,
        )
    );
    register_rest_field( $post_type_slug, $field_slug . '-longitude', array(
           'get_callback'    => 'get_address_longitude',
           'schema'          => null,
        )
    );
}
  
function get_address_latitude( $object ) {
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    $res = types_render_field($field_slug, array(
        'post_id'=> $post_id,
        'format' => 'FIELD_LATITUDE',
		'index' =>1,
    ));
    return $res;
}
  
function get_address_longitude( $object ) {
    $field_slug = 'direccion_comercio'; //replace direccion with your custom address field slug
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    $res = types_render_field($field_slug, array(
        'post_id'=> $post_id,
        'format' => 'FIELD_LONGITUDE',
		'index' =>1,
    ));
    return $res;
}

Above codes will output the first instance of custom address field "direccion_comercio" into REST API:
hidden link

"direccion_comercio-latitude":"40.444026","direccion_comercio-longitude":"-3.64478"
#1845721

It is possible add this code in /wp-json/wp/v2/comercios/ without specific ID?

#1846453

My issue is resolved now. Thank you!