I have a view to list places by distance and display them on a map. If the user does not enable their location I'd like to have the search use a somewhat dynamically generated default location (a guessed City, State, Country) that I insert via a shortcode "default_geo_location". I've read in the docs that you can set a map to render by inserting an address value. That does not seem to work when put in a geolocation block.
Is it possible to do this? Any suggestions or best practices?
Search:
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div id="distance-search-form"><div id="lump">[wpv-control-distance default_distance="10" default_unit="mi" compare_field="brewery-address" distance_center_url_param="toolset_maps_distance_center" distance_radius_url_param="toolset_maps_distance_radius" distance_unit_url_param="toolset_maps_distance_unit"][wpv-filter-submit name="Search"]</div></div>
[/wpv-filter-controls]
[wpv-filter-end]
Loop:
[wpv-geolocation message_when_missing="Please allow us to see your location. We must be allowed to access this information to be able to show you breweries near you!"]
[wpv-map-render map_id="map-2" address="[default_geo_location]" map_height="400px" single_zoom="16" map_type_control="off" street_view_control="off" cluster="on"][/wpv-map-render]
[/wpv-geolocation]
Hello,
I assume we are talking about the shortcode [wpv-map-render], and you are going to setup a default location in that shortcode to replace user's location,
If it is, There isn't such a built-in feature within shortcode [wpv-map-render].
See our document:
https://toolset.com/documentation/user-guides/maps-shortcodes/#wpv-map-render
No attribute "address" within above shortcode.
You can add a feature request for it:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
Our developers will evaluate it.
Hi,
Thank you for the documentation link. I looked at that, but I must have misunderstood the user guides talking about maps and geolocation.
Can I conditionally test for geolocation being enabled to decide if a map should be displayed or not?
Thanks!
I submitted the feature request to be able to apply a default location to either [wpv-map-render] or probably better to [wpv-geolocation]. This would be ideal.
In case it helps someone else I hacked together some proof-of-concept Javascript, in the Output Editor section of my View, to populate the search field and force the button click. The downside is this will force the browser to ask the user for location data as the page is loading. If you have this map on your homepage the user won't know if there is value sufficient to warrant them allowing it.
[wpv-filter-meta-html]
[wpv-layout-meta-html]
<script>
function getLocation() {
if (! (navigator.geolocation) ) {
console.log('No Geolocation');
populateSearchForm();
} else {
console.log('Geolocation is enabled');
navigator.geolocation.getCurrentPosition(showPosition, populateSearchForm);
}
}
function showPosition(position) {
console.log('Latitude: ' + position.coords.latitude);
console.log('Longitude: ' + position.coords.longitude);
if( $('#toolset-maps-distance-center').val() == '' ) {
$('#toolset-maps-distance-center').val(position.coords.latitude + ', ' + position.coords.longitude);
$('.wpv-submit-trigger').click();
}
}
function populateSearchForm() {
jQuery(document).ready(function(){
if( $('#toolset-maps-distance-center').val() != '' ) {
console.log('Search box already populated (' + $('#toolset-maps-distance-center').val() + ') so running with it...');
} else {
$('#toolset-maps-distance-center').val('[default_geo_location]');
$('.wpv-submit-trigger').click();
}
});
}
getLocation();
</script>
For the new question:
Can I conditionally test for geolocation being enabled to decide if a map should be displayed or not?
Yes, it is possible with shortcode [wpv-geolocation], see the document I mentioned above:
https://toolset.com/documentation/user-guides/maps-shortcodes/#wpv-geolocation
This shortcode will stop processing of everything that is placed inside it.
And thanks for share the custom JS codes, that will help other users.
No further support needed, thank you. Hopefully, the feature request can be implemented.