Using a latitude and longitude fields to position the map markers

The Maps layout (or the Multiple Entries Map widget and the Entry Map field) requires an address field on your form, which will be used as a source to geocode the coordinates to create the map markers. That Address field is, by default, a standard Gravity Forms address field type. You may use a different field type as the address source.

If you have the position coordinates (latitude and longitude) stored as two separate form fields, you may use them instead of the Address field to position the markers over the map.

A form, with three Gravity Forms text fields. One labelled Latitude, one Longitude, and the final labelled Map Icon.


If you wish to replace the Address field with a Latitude and a Longitude fields, these are the filters you'll need depending on which version of the Maps plugin you are using:


For GravityView Maps plugin version 3.1 and newer:

add_filter( 'gk/gravitymaps/markers/coordinates/fields-ids', static function( $fields_array = [], $view = null ) {

$form = $view->form->form;


/** Only use these fields if the form ID is 46 */

if ( 46 !== (int) $form['id'] ) {

return $fields_array;

}

/** IDs of the Latitude and Longitude fields */

return [ '3', '4' ];

}, 10, 2 );


For GravityView Maps plugin versions 3.0.x and older:

add_filter( 'gravityview/maps/markers/lat_long/fields_id', 'gk_lat_long_fields', 10, 2 );

function gk_lat_long_fields( $fields_array = array(), $gravityview_view = null ) {

$form = $gravityview_view->getForm();


/** Only use these fields if the form ID is 46 */

if ( 46 !== (int) $form['id'] ) {

return $fields_array;

}


/** IDs of the Latitude and Longitude fields */

return array( '6', '7' );

}


Here's an example that specifies different field IDs depending on which form is being called:

add_filter( 'gravityview/maps/markers/lat_long/fields_id', 'gk_maps_lat_long_fields', 10, 2 );

function gk_maps_lat_long_fields( $fields_array = array(), $gravityview_view = null ) {


$form = $gravityview_view->form->form;

$formid = (int) $form['id'];

switch ($formid) {

case 41: /** To use with form ID = 41 */

return array( '1', '2' );

break;

case 42: /** To use with form ID = 42 */

return array( '3', '4' );

break;

case 43: /** To use with form ID = 43 */

return array( '6', '7' );

break;

default:

return $fields_array;

}


}


Read here how to add these code samples to your website: Where to put code samples.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us