Gps tracking google map

I want to put the marker on the only path visited, not on any point on the map ... my code is like this

function initialize() { geocoder = new google.maps.Geocoder(); var myLatlng = new google.maps.LatLng(-25.363882,131.044922); var mapDiv = document.getElementById('map-canvas'); map = new google.maps.Map(mapDiv, { zoom: 15, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP }); google.maps.event.addListener(map, 'click', addNewPoint) } $(document).ready(function(){ initialize(); temp(); }); function reload() { window.location.href='<?=base_url()?>index.php/admin/tracking'; } function addNewPoint(e) { var data=""; if(i>0) { for (i in markersArray) { markersArray[i].setMap(null); } markersArray.length = 0; } $.ajax({ type: 'post', url: '<?=base_url()?>index.php/admin/tracking/get_last_location', // data: 'season_id='+season_id, dataType: "json", success: function(msg) { var newLatlng = new google.maps.LatLng(msg.lat,msg.lng); marker = new google.maps.Marker({map: map,position: e.latLng}); data+="Position :-"+e.latLng; geocoder.geocode({'latLng': e.latLng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[1]) { map.setZoom(13); infowindow.setContent(results[1].formatted_address); data+="\naddress :-"+results[1].formatted_address; infowindow.open(map, marker); } document.getElementById('info').value=data; } else { alert("Geocoder failed due to: " + status); } }); } }); markersArray.push(marker); i++; } function temp1(){ $.ajax({ type: 'post', url: '<?=base_url()?>index.php/admin/tracking/get_last_location', // data: 'season_id='+season_id, dataType: "json", success: function(msg) { var newLatlng = new google.maps.LatLng(msg.lat,msg.lng); map.panTo(newLatlng); document.getElementById('speed').value=msg.speed; if(myPoints.length>0) { var oldLatlng=myPoints[myPoints.length-1]; var myCoordinates = [ oldLatlng, newLatlng ]; if(oldLatlng!=newLatlng) { var myPath = new google.maps.Polyline({ path: myCoordinates, strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 2 }); way.push(myPath); myPath.setMap(map); } myPoints.push(newLatlng); } else { //var newLatlng = new google.maps.LatLng(value['lat'],value['long']); myPoints.push(newLatlng); } } }); setTimeout("temp1()",2000); } function deleteOverlays() { if (myPoints.length>0) { for (i in myPoints) { myPoints[i].setMap(null); } myPoints.length = 0; } if (way.length>0) { for (i in way) { way[i].setMap(null); } way.length = 0; } } 

but from this code I can only put the marker at one point. What can I do???

+4
source share
1 answer

I'm going to try to help, although many years have passed since I worked with Google maps.

The code to enter a new marker into the array is outside the success function, where a new marker is created. Also, if you initialized an array with values, I suspect that clicking a new value adds it to the array after all the initialized data.

But I'm not at all sure why this will give you any marker at all.

I suggest leaving an uninitialized array and then pushing a new marker inside the AJAX success function. Set up a map for the marker and any other information there, using the new marker object to make things very clear.

I am not 100% sure about the problem with an array adding an array that already matters, but I hope this information helps you find a solution.

+1
source

All Articles