Skip to content

Instantly share code, notes, and snippets.

@boldsupport
Last active October 11, 2017 16:50
Show Gist options
  • Save boldsupport/cc63e22c06c6f09da188 to your computer and use it in GitHub Desktop.
Save boldsupport/cc63e22c06c6f09da188 to your computer and use it in GitHub Desktop.
Mapply - Have the customer's location determine the centre of the map. Linked from: https://support.boldcommerce.com/hc/en-us/articles/207147066-Determine-the-Center-of-the-Map-by-Customer-Location
<script>
jQuery(document).ready(function(){
if(document.location.pathname.indexOf("iframe.php") == -1) {
useMyPosition();
} else {
var infoWindow = new google.maps.InfoWindow({map: map});
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
jQuery('.search_bar button').click();
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
}
});
function useMyPosition() {
var infoWindow = new google.maps.InfoWindow({map: bold_sl.map});
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
bold_sl.map.setCenter(pos);
jQuery('.search_bar button').click();
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment