Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Why does the Google Maps portion of my website only update when the user deletes his/her cookies?

$
0
0

I'm using the Maps JavaScript API and am putting a map with center being in some set latitude and longitude. I'm trying to set the default coordinates to be the user's location in the end, but on testing certain set coordinates, the page only seems to change coordinates when the user deletes his/her cookies and refreshes the page. How can I make it so that the user does not have to delete their cookies to receive the latest coordinates from JavaScript?

My code that does not update as desired:

var map;
function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -34.397, lng: 150.644},
      zoom: 8
    });
}

Edit: I'm changing it manually and refreshing the page for now, but later I would automatically set location using

<div id="map" style="height: 400px; width: 100%;"></div>
<script>
var map;
var default_coords;
navigator.geolocation.getCurrentPosition(function(position) {
  default_coords = {lat: position.coords.latitude, lng: position.coords.longitude};
});

function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: default_coords,
      zoom: 8
    });
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&callback=initMap" async defer></script>

The problem is that the map location does not change even if I change it in the code.


Viewing all articles
Browse latest Browse all 138163

Trending Articles