Tengo un problema con la api de google maps con la geolocalización. quiero hacer funcionar un botón que dice "Ubicación actual" y me de mi dirección actual. estoy utilizando javascript puro, lo que no logre hacer es hacer funcionar la etiqueta button en javascript intente de todas formas pero no logro encontrar la correcta.
// geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Tu ubicacion');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
infoWindow = new google.maps.InfoWindow;
// geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Tu ubicacion');
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: El servicio de Geolocalizacón ha fallado' :
'Error: Su navegador no admite Geolocalización.');
infoWindow.open(map);
}
}
}
<button onclick="getLocation()">Ubicación actual</button>
<div id="map"></div>