Last active
October 11, 2017 16:50
-
-
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
This file contains 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
<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