javascript - marker disappears after setPosition on google maps -
i worked on gps position show on google maps. issue here google mark appears on first time (set google marker part). disappeard every time after setposition method called. appreciated.
<!doctype html> <html> <head> <script src="http://maps.google.com/maps/api/js?sensor=false"> </script> <script type="text/javascript"> var index=0; var latitudes = new array(35.71434,35.715612,35.711779,35.70913,35.710646); var longitudes = new array(-77.92854,-77.924076,-77.925171,-77.925943,-77.930278); var mapoptions; var coords = new google.maps.latlng(latitudes[index],longitudes[index]); var marker = null; var map; var newpoint; function autoupdate(){ navigator.geolocation.getcurrentposition(function(position){ mapoptions = { zoom: 15, center: coords, maptypecontrol: true, navigationcontroloptions: { style: google.maps.navigationcontrolstyle.small }, maptypeid: google.maps.maptypeid.roadmap }; map = new google.maps.map( document.getelementbyid("mapcontainer"), mapoptions ); if(marker){ newpoint = new google.maps.latlng(latitudes[index],longitudes[index]); marker.setposition(newpoint); } else{ marker = new google.maps.marker({ position: coords, map: map, }); } if(newpoint!=null){ map.setcenter(newpoint); marker.setvisible(true); } }); index=index+1; if(index==5){ index=0; } settimeout(autoupdate,5000); } autoupdate(); </script> <style type="text/css"> #mapcontainer { height: 500px; width: 800px; border:10px solid #eaeaea; } </style> </head> <body> <div id="mapcontainer"></div> </body> </html>
thank help.
move code creates map out of autoupdate function, run once on page load:
mapoptions = { zoom: 15, center: coords, maptypecontrol: true, navigationcontroloptions: { style: google.maps.navigationcontrolstyle.small }, maptypeid: google.maps.maptypeid.roadmap }; map = new google.maps.map( document.getelementbyid("mapcontainer"), mapoptions );
or similar marker, create once, if has been initialized/created, change center.
Comments
Post a Comment