plum
11/30/2018 - 7:49 PM

A-Towing Geolocation

Allows users to send their latitude & longitude through a contact form.

// get submit location button
var submitLocationBtn = document.getElementById('submitLocation');

// get form field for lat
var lat = document.getElementById("form-field-lat");
// get form field for lng
var lng = document.getElementById("form-field-lng");
	
	function getLocation() {
	    if (navigator.geolocation) {
	        navigator.geolocation.getCurrentPosition(showPosition);
	    } else {
	        alert("Geolocation is not supported by this browser.");
	    }
	}
	function showPosition(position) {
	    lat.value = position.coords.latitude;
		lng.value = position.coords.longitude; 
	}

// listen for clicks on submitLocationBtn
submitLocationBtn.addEventListener('click', (e) => {
	e.preventDefault();
	getLocation();
	
})