Frontend Submission - Google Maps - Custom Binding

Support MB Frontend Submission Frontend Submission - Google Maps - Custom Binding

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #45746
    CodogCodog
    Participant

    Hi there,
    I have a frontend submission form that contains a Google Map and address field that uses the Geocoding API for autocomplete. All works as expected!

    However, I don't seem to be able to get the "Custom Binding" working as documented here

    Here is my code with an attempt to bind the 'City' & 'Country' to a hidden custom field:

    			
      // Map field.
      [
      'id'            => 'location_map',
      'type'          => 'map',
      'api_key'       => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      'std'           => '51.5074,-0.1278,15',
      'address_field' => 'location_address',
      ],
      // Address field.
      [
      'id'   => 'location_address',
      'name' => __( 'Address', 'text-domain' ),
      'type' => 'text',
      'placeholder'   => 'Enter address...',
      'desc'     => __( 'Enter the complete address.', 'text-domain' ),
      'required' => true,
      ],
      // Hidden field to store the city and country
      [
      'id'   => 'city_country',
      'type' => 'hidden',
      'binding' => 'city + " " + country',
      'std'  => 'null',
      ],
    

    Expected result: Autocomplete - City and Country values to be added to hidden field.
    Actual result: Returns nothing?

    I have also tried this with a plain text field and the result is the same?

    What am I missing?

    Thanks in advance 🙂

    #45752
    PeterPeter
    Moderator

    Hello,

    Do you add the setting geo to the field group settings? Please follow the documentation https://docs.metabox.io/extensions/meta-box-geolocation/#field-group-settings

    #45753
    CodogCodog
    Participant

    Hi Peter, thanks for the response. Yeah this was the bit I was unclear about? I assume you mean adding this:

    
    'geo' => [
        'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxx',
    ],
    

    To some field group? Do you mean I need to wrap my "Map Field, Address Field , Hidden field" in a "Field Group and add the above? Not clear to me?

    As I am strictly doing this with code I would be grateful if you could show me a simple example?

    #45761
    PeterPeter
    Moderator

    Hello,

    If you use the code to register the meta box (field group in the builder UI), you need to add geo to the meta box settings. For example:

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $meta_boxes[] = [
            'title'      => 'Event details',
            'post_types' => 'event',
            'geo' => [
                'api_key' => 'YOUR API KEY',
            ],
            'fields'     => [
                ...
            ],
        ];
    
        return $meta_boxes;
    } );

    https://docs.metabox.io/creating-fields-with-code/

    #48251
    CodogCodog
    Participant

    Hi there,
    I never got this working. I have re-visited your suggestion (from above):

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $meta_boxes[] = [
            'title'      => 'Event details',
            'post_types' => 'event',
            'geo' => [
                'api_key' => 'YOUR API KEY',
            ],
            'fields'     => [
                ...
            ],
        ];
    
        return $meta_boxes;
    } );

    But this binding never seems to work for me?

    1. I have set up an unrestricted API key with all available API's.
    2. I have added the geo parameter to my Metabox (Exactly as suggested):
    3. My full test implementation is like this:

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $meta_boxes[] = [
            'title'      => 'Event details',
            'post_types' => 'event',
            'geo' => [
                'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',// Unrestricted API Key
            ],
            'fields'     => [
                // Map field.
                [
                'id'            => 'location_map',
                'type'          => 'map',
                'api_key'       => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                'std'           => '51.5074,-0.1278,15',
                'address_field' => 'location_address',
                ],
                // Address field.
                [
                'id'   => 'location_address',
                'name' => __( 'Address', 'text-domain' ),
                'type' => 'text',
                'placeholder'   => 'Enter address...',
                'desc'     => __( 'Enter the complete address.', 'text-domain' ),
                'required' => true,
                ],
                // Hidden field to store the city and country
                [
                'id'   => 'city_country',
                'type' => 'hidden',
                'binding' => 'city + " " + country',
                'std'  => 'null',
                ],
            ],
        ];
    
        return $meta_boxes;
    } );

    I am using a clean install of WP:

    - WP 6.8.1
    - Metabox Version 5.10.9
    - AIO Version 2.1.1
    - PHP 8.3
    - MYSQL 8.0.35

    I have the MB Geolocation extension on.
    I see no console log errors.
    I see no debug.log errors.

    Can you confirm that this implementation works for you? (or Not). I cannot get this feature to work for love or money?

    Thanks.

    #48255
    PeterPeter
    Moderator

    Hello,

    It works properly on my demo site. Please note that the city field is not supported when using Google Maps. You should use locality instead of city. Following the documentation https://docs.metabox.io/extensions/meta-box-geolocation/#other-fields
    And check my screen record https://drive.google.com/file/d/1dZqoujJ89l3wUoPsHU_p5f4f6jl4faQA/view?usp=sharing

    #48258
    CodogCodog
    Participant

    Thanks for the clarification Peter!

    That was really helpful 🙂

    I will try this myself.

    #48343
    CodogCodog
    Participant

    Hi Peter,
    I have returned to look at this again, watched your video, and double checked the documentation. But it still does not work for me via a coded solution. Here is my implementation:

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $meta_boxes[] = [
            'title'      => 'Event details',
            'post_types' => 'event',
            'geo' => [
                'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',// As per your documentation only one API key required. Not necessary on the 'location_map' field.
            ],
            'fields'     => [
                // Map field.
                [
                'id'            => 'location_map',
                'type'          => 'map',
                'std'           => '51.5074,-0.1278,15',
                'address_field' => 'location_address',
                ],
                // Address field.
                [
                'id'   => 'location_address',
                'name' => __( 'Address', 'text-domain' ),
                'type' => 'text',
                'placeholder'   => 'Enter address...',
                'desc'     => __( 'Enter the complete address.', 'text-domain' ),
                'required' => true,
                ],
                // Hidden field to store the city and country
                [
                'id'   => 'city_country',
                'type' => 'hidden',
                'binding' => 'locality + " " + country',
                'std'  => 'null',
                ],
                // Other custom fields....
            ],
        ];
    
        return $meta_boxes;
    } );

    When I follow this implementation:

    1. My Google map breaks and shows the standard "Sorry! Something went wrong" ? (IE: InvalidKeyMapError in console).
    2. The Autocomplete feature on the address field stops working?
    3. The hidden field can never capture any "binding" as 1&2 above are broken?

    As before:

    - WP 6.8.1
    - Metabox Version 5.10.10
    - AIO Version 2.1.1
    - PHP 8.3
    - MYSQL 8.0.35

    I have the MB Geolocation extension on.
    I see no console log errors (Apart from the InvalidKeyMapError mentioned above).
    I see no debug.log errors.
    I am using a completely unrestricted API key from my Google cloud console for testing.

    Although I appreciate the video showing this feature working in the Metabox builder. I would be very grateful if you can test a coded version as above to confirm this works on your end. I am stumped!!!

    I look forward to your response 🙂

    #48346
    PeterPeter
    Moderator

    Hello,

    I suggest you try steps below to troubleshoot the issue:

    1. Create a new field group, add a Google Map field and an address field; no need to use the Geolocation feature.
    2. Add your API key to the map field and check if the map field works properly.

    => If yes, you can try to add the API key to the map field in the field group that you use the Geolocation and recheck this.

    => If not, your API key isn't correct or some services are not enabled for using the Geolocation.
    https://docs.metabox.io/extensions/meta-box-geolocation/#geolocation-api

    #48348
    CodogCodog
    Participant

    Hi Peter,
    I have already checked everything you have mentioned in your previous post. I have the correct API services enabled.

    Selected APIs:
    - Geocoding API
    - Maps JavaScript API
    - Places API

    My API key is 100% valid.

    My Address and Map fields work absolutely fine and exactly as expected. As soon as I use the:

    'geo' => [
        'api_key' => 'YOUR API KEY',
    ],

    I my field group both the Address and Map fields break!

    Again I would urge you to test a coded version.

    Cheers

    #48357
    PeterPeter
    Moderator

    Hello,

    This information is noted in the documentation https://docs.metabox.io/extensions/meta-box-geolocation/#field-group-settings

    If you have a Google Maps field in your field group, then you can set the Google API key once, either for the Geolocation extension (as above) or for the map field. No need to enter the same key for both of them.

    In this case, you can add the API key to the map field settings and use this 'geo' => true for the meta box settings. Also, the address field must start with address_xxx instead of location_address

    This code works properly on my local site

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $meta_boxes[] = [
            'title'      => 'Event details',
            'post_types' => 'page',
            'geo' => true,
            'fields'     => [
                // Map field.
                [
                'id'            => 'location_map',
                'type'          => 'map',
                'std'           => '51.5074,-0.1278,15',
                'address_field' => 'location_address',
                'api_key' => 'xxxxxxxx'
                ],
                // Address field.
                [
                'id'   => 'address_location',
                'name' => __( 'Address', 'text-domain' ),
                'type' => 'text',
                'placeholder'   => 'Enter address...',
                'desc'     => __( 'Enter the complete address.', 'text-domain' ),
                'required' => true,
                ],
                // Hidden field to store the city and country
                [
                'id'   => 'city_country',
                'type' => 'text',
                'binding' => 'locality + " " + country',
                'std'  => 'null',
                ],
                // Other custom fields....
            ],
        ];
    
        return $meta_boxes;
    } );
    #48365
    CodogCodog
    Participant

    Hi Peter,
    thanks for the more detailed explanation. Following your coded method above does work, thank you! However, your instructions are definitely not inline with your documentation here: https://docs.metabox.io/extensions/meta-box-geolocation/

    For example your documentation states:

    1. Under the "Open Street Maps" section to use:

    'geo' => true,

    In the field group settings?

    But under the "Google Maps" section to use:

    'geo' => [
        'api_key' => 'YOUR API KEY',
    ],

    In the field group settings?

    2. Under the "Custom Binding" to get the "City & Country" to use:

    'binding' => 'city + " " + country', // returns QLD Australia

    When you have stated to use "Locality"? (Which does work).

    These things do not match what you have demonstrated above for Google Maps in your coded example? It's one of the reasons why I found, what should have been a straightforward task so tricky?

    Many Thanks 🙂

    #48370
    PeterPeter
    Moderator

    Hello,

    1. Yes, but we already have a note in the documentation

    If you have a Google Maps field in your field group, then you can set the Google API key once, either for the Geolocation extension (as above) or for the map field. No need to enter the same key for both of them.

    You can add the API key to the map field and enable 'geo' => true, for the field group settings. I will inform the content team to update the documentation.

    2. The field city is supported when using Open Street Map (OSM) instead of Google Maps. So the documentation is still valid. Following the documentation https://docs.metabox.io/extensions/meta-box-geolocation/#address-field

    #48372
    CodogCodog
    Participant

    Yes, I agree all the information is in the documentation. However, the order and presentation of these concepts are (in my opinion), not well structured and clear.

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.