Support

Account

Home Forums ACF PRO Auto center Google Map to current location

Solved

Auto center Google Map to current location

  • Using a Google Map custom field on the frontend, is there a way to automatically find the current location by default? So the user doesn’t have to click the “Find current location” button or, in other words, the default center of the map is the user’s current location?

    Thanks for any help in advance!

  • Hi @emiel

    You could write some JS to trigger a click on the locate button by default like so:

    <script type="text/javascript">
    (function($) {
    	
    	acf.on('ready', function(){
    		
    		$('.field[data-key="field_123"] a[data-name="find-location"]').trigger('click');
    		
    	})
    	
    })(jQuery);	
    </script>

    Cheers
    E

  • Great! Thank you very much!

  • Hi Elliot,

    Could this piece of code also be modified (with a cookie placement for instance) so it auto-triggers the find-location-function only if there’s been no location set yet? So if the user enters a city name in the field and returns to the page later, the find-location function won’t be triggered? Or that it’s only triggered on first visit?

    Thanks for your reply in advance!

    Kind regards, Emiel.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.