javascript - Show only the current location marker using watchPosition -
I am trying to create a single marker on Google Maps to show the user's current location. Using the following code I'm able to do this, but every time watchPosition refreshes it makes a new point. How can I clear past markers each time a new point is added?
// Geolocation var won = function (status) {var iconimage = new google.maps.MarkerImage ('images / current_location_small .png', new google.maps.size (15, 15) , New google.maps.Point (0,0), new google.maps.Point (7, 7)); Var lat = position.coords.latitude; Var tall = position.coords.longitude; Var myLatlng = new google.maps.LatLng (latitude, long); Var marker = new google.maps.marker ({status: myLatlng, map: map, icon: iconimage}); Marker.setMap (map); }; Var watchID = navigator.geolocation.watchPosition (win); You can declare your marker out of the function and setPosition's (LatLng) method // Geolocation Verifier Marker = Faucet; Var win = function (status) {var lat = position.coords.latitude; Var tall = position.coords.longitude; Var myLatlng = new google.maps.LatLng (latitude, long); If (marker == tap) {var iconimage = new google.maps.MarkerImage ('images / current_location_small.png', new google.maps.size (15, 15), new google.maps.Point (0,0), New google.maps.Point (7, 7)); Marker = new google.maps.marker ({status: myLatlng, map: map, icon: iconimage}); Marker.setMap (map); } Other {marker.setPosition (myLatlng); }}; Var watchID = navigator.geolocation.watchPosition (win);
Comments
Post a Comment