JS
-
-
Save philcon93/4124670ad9ab1672f09ee37205693eb9 to your computer and use it in GitHub Desktop.
Add users location to store finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// check for Geolocation support | |
if (navigator.geolocation) { console.log('Geolocation is supported!'); | |
}else { console.log('Geolocation is not supported for this Browser/OS.'); } | |
window.onload = function() { | |
var startPos; | |
var geoOptions = { | |
timeout: 10 * 1000 | |
} | |
var geoSuccess = function(position) { | |
startPos = position; | |
var lat = startPos.coords.latitude; | |
var long = startPos.coords.longitude; | |
var geocoder = new google.maps.Geocoder; | |
var latlng = {lat: parseFloat(lat), lng: parseFloat(long)}; | |
var postcode; | |
geocoder.geocode({'location': latlng}, function(results, status) { | |
if (status === 'OK') { | |
if (results[0]) { | |
var addressComponents = results[0].address_components; | |
for (var i = 0; i < addressComponents.length; i++) { | |
var type = addressComponents[i].types[0]; | |
if(type == "postal_code"){ | |
postcode = addressComponents[i].short_name; | |
updateStoreFinder(postcode); | |
} | |
} | |
} | |
} | |
}); | |
}; | |
var geoError = function(error) { | |
console.log('Error occurred. Error code: ' + error.code); | |
// error.code can be: | |
// 0: unknown error | |
// 1: permission denied | |
// 2: position unavailable (error response from location provider) | |
// 3: timed out | |
}; | |
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions); | |
}; | |
var updateStoreFinder = function(postcode){ | |
if(postcode){ | |
$('#geo_zip').val(postcode); | |
$('#geo_search').trigger('click'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment