boldsupport
1/4/2016 - 5:36 PM

Mapply - Have the customer's location determine the centre of the map.

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>