I am trying to use the API
of Google Maps so that I can paint a map according to the coordinates that happened to it, I do it with the method geolocation
of HTML5
, but nothing appears on the web. I have tried the parameters that come by default in the Google documentation and it does not work either. The code is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tu Aplicación del Tiempo</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<h1>El Tiempo</h1>
<h2 id="messageShow">Geolocalizando tú posición, espera un momento...</h2>
<div id="map">
</div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap"></script>
<script src="js/app.js"></script>
</html>
//JavaScript
(function Prueba(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getCoords, errorFound)
} else {
alert("Upgrade your browser");
}
function errorFound(error) {
alert("Error: " + error.code );
}
function getCoords(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
console.log("Your position is: " + lat + "," + lon);
$.getJSON({
url: WEATHER_API_URL + "&lat=" + lat + "&lon=" + lon
}, getCurrentWeather, initMap)
}
function initMap(){
var map;
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: lat, lng: lon},
zoom: 8
});
}
})();
There is a call AJAX
to a API
of the time that does not have to do with the question, but there if the geolocation works. The console gives me the following error: InvalidValueError: initMap is not a function
I hope you help me with this error. Thank you. Juan.