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);
}
}One glaring issue:
infoWindow = new google.maps.InfoWindow;
You never declare the variable, and you need the constructor when calling it, so...
var infoWindow = new google.maps.InfoWindow();
All of this should be visible as errors in your javascript console. Are you not using browser Dev tools to debug? Edit: Stupids
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
-
One glaring issue:
infoWindow = new google.maps.InfoWindow;
You never declare the variable, and you need the constructor when calling it, so...
var infoWindow = new google.maps.InfoWindow();
All of this should be visible as errors in your javascript console. Are you not using browser Dev tools to debug? Edit: Stupids
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
Javascript doesn't require that variables are declared before they're used, unless you enable "strict" mode[^]. It also doesn't always require the parentheses when calling a constructor. For example,
foo = new Date;
seems to work fine without any errors or warnings. Basically, Javascript is the VB6 of the web. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Javascript doesn't require that variables are declared before they're used, unless you enable "strict" mode[^]. It also doesn't always require the parentheses when calling a constructor. For example,
foo = new Date;
seems to work fine without any errors or warnings. Basically, Javascript is the VB6 of the web. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Haven't worked outside of strict mode in ages, forgot about that bunk.
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
-
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);
}
}