Bonjour,
Voici un bout de code que j'utilise pour géolocaliser :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
<script>
// Position par défaut
var centerpos = new google.maps.LatLng(48.579400,7.7519);
// Ansi que des options pour la carte, centrée sur latlng
var optionsGmaps = {
center:centerpos,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 15
};
// Initialisation de la carte avec les options
var map = new google.maps.Map(document.getElementById("map"), optionsGmaps);
if(navigator.geolocation) {
// Fonction de callback en cas de succès
function affichePosition(position) {
var infopos = "Position déterminée : <br>";
infopos += "Latitude : "+position.coords.latitude +"<br>";
infopos += "Longitude: "+position.coords.longitude+"<br>";
infopos += "Altitude : "+position.coords.altitude +"<br>";
document.getElementById("maposition").innerHTML = infopos;
// On instancie un nouvel objet LatLng pour Google Maps
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// Ajout d'un marqueur à la position trouvée
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Vous êtes ici"
});
map.panTo(latlng);
}
// Fonction de callback en cas derreur
function erreurPosition(error) {
var info = "Erreur lors de la géolocalisation : ";
switch(error.code) {
case error.TIMEOUT:
info += "Timeout !";
break;
case error.PERMISSION_DENIED:
info += "Vous navez pas donné la permission";
break;
case error.POSITION_UNAVAILABLE:
info += "La position na pu être déterminée";
break;
case error.UNKNOWN_ERROR:
info += "Erreur inconnue";
break;
}
document.getElementById("maposition").innerHTML = info;
}
navigator.geolocation.getCurrentPosition(affichePosition,erreurPosition);
} else {
alert("Ce navigateur ne supporte pas la géolocalisation");
}
</script> |
Créer un conteneur :
<div id="id_conteneur_map" style="width:640px; height:480px"></div>
Charger l'API Google maps :
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
Et le plus important, la documentation :
http://www.w3schools.com/html/html5_geolocation.asp
Partager