谷歌地图与GPS跟踪

时间:2011-07-13 13:15:17

标签: google-maps

我想将标记放在唯一访问过的路径上,而不是放在任何地图上......我的代码就像这样

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 个答案:

答案 0 :(得分:1)

我会尽力帮助,尽管自从我使用谷歌地图以来已经有好几年了。

将新标记推入数组的代码不在创建新标记的成功函数之外。另外,如果您使用值初始化了数组,我怀疑在您已初始化的所有数据之后,推送一个新值会将其添加到数组中。

但我完全不确定为什么这会给你任何标记。

我建议将数组保持未初始化状态,然后在AJAX成功函数中推送新标记。使用新的标记对象设置标记的地图和其他任何信息,以使事情变得非常清晰。

我不是100%确定附加到已经有值的数组的数组推送问题,但我希望这些信息可以帮助您找到解决方案。