Maps: How can I use the latitude and longitude form fields to position the map markers?

The Maps Premium View (or the Map widget and the Entry Map field) requires to setup an address form field as the source to geocode 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 form fields, you may also use them instead of the Address 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.

This is how these fields will look to the user (with some values filled-in):

The form, with longitude and latitude values filled in, and a custom map icon of a green flag picked.

When using the Maps Premium View extension, in order to override the Address Field setting replacing it by the coordinates fields, you can use the filter  gravityview/maps/markers/lat_long/fields_id to use the passed longitude and latitude as the coordinates for the entry.

Then when the map is rendered, the coordinates will be pulled from the Longitude and Latitude fields:

Map showing the marker placed in Cape Canaveral, Florida (the city that the coordinates referred to)

In the form shown above in this article, the form ID is "46", the Latitude field ID is "6" and the Longitude field ID is "7", so we would use the following code:

/**
 * Use the coordinates (Latitude and Longitude) instead of the address to position the markers on the maps
 * 
 * @see https://docs.gravityview.co/article/300-how-can-i-use-the-latitude-and-longitude-form-fields-to-position-map-markers
 *
 * @param array $fields_array Gravity Forms fields IDs containing the latitude and longitude
 * @param GravityView_View $gravityview_view Current View object
 *
 * @return array Array with field IDs of latitude and longitude fields (Example: [ 5, 6 ] ). Empty array if not the form we want to override.
 */

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

	$form = $gravityview_view->getForm();

	// Only use these fields to provide the coordinates for Form ID #46
	if ( 46 !== (int) $form['id'] ) {
		return $fields_array;
	}

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

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

Here's an example of several forms with different field IDs:

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

	$form = $gravityview_view->getForm();
	$formid = (int) $form['id'];
	
	switch ($formid) {
	    case 41: // form ID = 41
	        return array( '1', '2' );
	        break;
	    case 42: // form ID = 42
	        return array( '3', '4' );
	        break;
	    case 43: // form ID = 43
	        return array( '6', '7' );
	        break;
	    default:
	       return $fields_array;
	}

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

Modify the code below for your own site

Modify the code below with your own form ID, longitude field ID and latitude field id:

/**
 * Use the coordinates (Latitude, Longitude) instead of the address to position the markers over the Maps
 * 
 * Replace 'MY_LATITUDE_FIELD_ID', 'MY_LONGITUDE_FIELD_ID' with field ids containing the latitude and longitude values
 *
 * @param array $fields_array Gravity Forms fields IDs containing the latitude and longitude
 * @param GravityView_View $gravityview_view Current View object
 *
 * @return array Array with field IDs of latitude and longitude fields (Example: [ 5, 6 ] ). Empty array if not the form we want to override.
 */
function my_gv_maps_lat_long_fields( $fields_array = array(), $gravityview_view = null ) {

	$form = $gravityview_view->getForm();

	// Only use these fields to provide the coordinates for Form ID #123
	if ( 123 !== (int) $form['id'] ) {
		return $fields_array;
	}

	return array( 'MY_LATITUDE_FIELD_ID', 'MY_LONGITUDE_FIELD_ID' );
}

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

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