Vous avez trouvé ce tutoriel intéressant ? Recommandez le en cliquant sur le bouton +1    

Géolocalisation des visiteurs

Tutoriel publié le Mis à jour le
Tutoriel en Français

Cette fonction vous permet de géolocaliser un visiteur.

Attention :

La géolocalisation n'est possible qu'après accord explicite du visiteur.

Un message apparaît en haut de la page consultée demandant au visiteur s'il accepte ou refuse de partager sa position.

En cas de refus, la carte ne s'affiche pas.

geoLatlng

$('#maCarte').gmap3(
	{
		action : 'geoLatLng',
		callback : function(latLng){
			if (latLng){
				$('#resultat-geolocalisation').html('Vous avez été géolocalisé avec succès !');
				$(this).gmap3(
					{
						action: 'addMarker', 
						latLng:latLng
					},
					"autofit"
				);
			} else {
				$('#resultat-geolocalisation').html('Impossible de vous géolocaliser !');
			}
		}
	}
);

Carte avec geoLatlng

Code complet avec geoLatlng

<!DOCTYPE html>
<html lang="fr">
	<head>
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
		<meta charset="UTF-8"/>
		<title>Votre titre de page</title>
		<style type="text/css">
			html{height:100%}
			body{height:100%;margin:0px;padding:0px}
			#maCarte{height: 100%;}
		</style>
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
		<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
		<script type="text/javascript" src="http://www.votredomaine.fr/js/gmap3.js"></script>
		<script type="text/javascript"> 
			$(function(){
				$('#maCarte').gmap3(
					{
						action : 'geoLatLng',
						callback : function(latLng){
							if (latLng){
								$('#resultat-geolocalisation').html('Vous avez été géolocalisé avec succès !');
								$(this).gmap3(
									{
										action: 'addMarker', 
										latLng:latLng
									},
									"autofit"
								);
							} else {
								$('#resultat-geolocalisation').html('Impossible de vous géolocaliser !');
							}
						}
					}
				);
			});
		</script>
	</head>
	
	<body>
		<div id="resultat-geolocalisation"></div>
		<div id="maCarte"></div>
	</body>
</html>

 

Source

geoLatlng

Résumé

GMap3 Plugin JQuery : geoLatlng

geoLatlng