google map et watchposition

Par wenz, il y a 8 ans


Bonjour,
je suis entrain recuperé la position d'un user et je l'affiche sur google map et je voudrais mettre à jour le maker à chaque changement de position de l'user et je le fais avec watchposition, mais la position du maker sur le map ne change pas.
voici mon code

navigator.geolocation.getCurrentPosition(onSuccess, onError, {maximumAge: 0, timeout: Infinity, enableHighAccuracy:true}); function onSuccess(position) { var nom=logUser.User.username; //Google Maps var googleLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var mapOptions = {zoom:17,center: googleLatLng,streetViewControl: true,disableDefaultUI: true} var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); addMarker(map, googleLatLng, nom, "<b>"+nom+"</b> | MEDIC CONSEILS"); //var marker = new google.maps.Marker({position: myLatlng,map: map}); $.jStorage.set('askUser',googleLatLng); var userPos = $.jStorage.get("askUser"); } function onError(error) { errorBox("le temps d'attente est dépassé, veuillez réessayer"+'\n'); } function addMarker(map, googleLatLng, title, content){ var markerOptn={position:googleLatLng,map:map,title:title,animation:google.maps.Animation.DROP }; var marker=new google.maps.Marker(markerOptn); var infoWindow=new google.maps.InfoWindow({ content: content, position: googleLatLng}); google.maps.event.addListener(marker, "click", function(){ infoWindow.open(map); }); } var watchID = navigator.geolocation.watchPosition(function(position) { // Get current position pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); // Redefine current location marker if (typeof(marker) != "undefined") marker.setMap(null); var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'; marker = new google.maps.Marker({ position: pos, map: map, title: 'you are here', icon: image },watchOptions); })

j'ai fait une erreur quelque part ou ce n'est pas la bonne façon de faire?
Merci

2 réponses

Ayele Anenou, il y a 8 ans

Bonjour,
Personne pour m'aider? Svp

Mirzo, il y a 8 ans

Bonjour,
Je pense qu'il faut que tu stock le marker que tu créer la première fois dans une variable global pour changer sa position par la suite avec marker.setPosition(newPosition);
ça donnerais quelque chose comme ça avec la variable userMarker :

navigator.geolocation.getCurrentPosition(onSuccess, onError, {maximumAge: 0, timeout: Infinity, enableHighAccuracy:true}); var userMarker; function onSuccess(position) { var nom=logUser.User.username; //Google Maps var googleLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var mapOptions = {zoom:17,center: googleLatLng,streetViewControl: true,disableDefaultUI: true} var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); addMarker(map, googleLatLng, nom, "<b>"+nom+"</b> | MEDIC CONSEILS"); //var marker = new google.maps.Marker({position: myLatlng,map: map}); $.jStorage.set('askUser',googleLatLng); var userPos = $.jStorage.get("askUser"); } function onError(error) { errorBox("le temps d'attente est dépassé, veuillez réessayer"+'\n'); } function addMarker(map, googleLatLng, title, content){ var markerOptn={position:googleLatLng,map:map,title:title,animation:google.maps.Animation.DROP }; var marker=new google.maps.Marker(markerOptn); var infoWindow=new google.maps.InfoWindow({ content: content, position: googleLatLng}); google.maps.event.addListener(marker, "click", function(){ infoWindow.open(map); }); } var watchID = navigator.geolocation.watchPosition(function(position) { // Get current position pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); // Redefine current location marker if (typeof(marker) != "undefined") marker.setMap(null); var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'; if(userMarker){ userMarker.setPosition(pos); }else { userMarker = new google.maps.Marker({ position: pos, map: map, title: 'you are here', icon: image },watchOptions); } })

C'est une proposition d'après ce que je comprend de ton code, je ne dis pas que c'est la solution exacte à ton problème ^^

Quoi qu'il en soit je pense qu'il faut que tu utilise marker.setPosition(newPosition) pour mettre à jour la position de ton marker.