2011-07-13 121 views
4

我希望把標記的唯一訪問路徑上沒有任何點到地圖...我這樣的代碼谷歌地圖與GPS跟蹤

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; 
} 

} 

但是從這個代碼我只能把標記在一個點。我能做什麼???

回答

1

雖然距離我的Google地圖工作已有多年,但我會盡力幫忙。

將新標記推入數組的代碼不在創建新標記的成功函數之外。另外,如果你已經用數組初始化了數組,我懷疑在你已經初始化的所有數據之後,推新值會將它添加到數組中。

但我完全不知道爲什麼這會給你任何標記。

我建議讓數組保持未初始化狀態,然後在AJAX成功函數中推入新標記。使用新的標記對象爲標記和其他任何信息設置地圖,以使事情變得非常清晰。

我不是100%肯定追加到已有值的數組的數組推送問題,但我希望這些信息可以幫助您找到解決方案。