Problem with Geolocation
-
This is the first time i've posted on this website, so if i'm doing something wrong just let me know. Anyways, I'm having a problem getting my geolocation to work. I absolutely cannot figure out what the problem is. Without the geolocation bit of code, the default location, markers, and such work fine. Then when I try to add in the geolocation the map won't appear at all and just the button appears.
var map; function initMap() { var myLatLng = {lat: 41.413118, lng: -82.072537} map = new google.maps.Map(document.getElementById('map'), { center: myLatLng, zoom: 18 }); var marker = new google.maps.Marker({ position: myLatLng, map: map, title: 'My Location' }); var marker = new google.maps.Marker({ position: {lat: 41.412726, lng: -82.072047}, map: map, title: 'PS Building', label: {text:'PS Building'} }); infoWindow = new google.maps.InfoWindow; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = { lat: position.coords.latitude, lng: position.coords.longitude }; var marker = new google.maps.Marker({ position: pos, map: map, title: 'Your Location' }); infoWindow.setPosition(pos); infoWindow.open(map); map.setCenter(pos); }, function() { handleLocationError(true, infoWindow, map.getCenter()); }); } else { // Browser doesn't support Geolocation handleLocationError(false, 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.');
infoWindow.open(map);
}
} -
This is the first time i've posted on this website, so if i'm doing something wrong just let me know. Anyways, I'm having a problem getting my geolocation to work. I absolutely cannot figure out what the problem is. Without the geolocation bit of code, the default location, markers, and such work fine. Then when I try to add in the geolocation the map won't appear at all and just the button appears.
var map; function initMap() { var myLatLng = {lat: 41.413118, lng: -82.072537} map = new google.maps.Map(document.getElementById('map'), { center: myLatLng, zoom: 18 }); var marker = new google.maps.Marker({ position: myLatLng, map: map, title: 'My Location' }); var marker = new google.maps.Marker({ position: {lat: 41.412726, lng: -82.072047}, map: map, title: 'PS Building', label: {text:'PS Building'} }); infoWindow = new google.maps.InfoWindow; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = { lat: position.coords.latitude, lng: position.coords.longitude }; var marker = new google.maps.Marker({ position: pos, map: map, title: 'Your Location' }); infoWindow.setPosition(pos); infoWindow.open(map); map.setCenter(pos); }, function() { handleLocationError(true, infoWindow, map.getCenter()); }); } else { // Browser doesn't support Geolocation handleLocationError(false, 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.');
infoWindow.open(map);
}
}Why are you repeating this question? It's been answered, below. (Though you may also need to put
'Error: Your browser doesn\\'t support
geolocation.');
on one line first, if that's not just a formatting issue on this site.) And your
onclick="GetLocation();"
attached to the button won't do anything unless you define a function with that name in your script.