谷歌api一段时间工作适当,有些时候没有

时间:2019-06-05 06:03:54

标签: javascript php ajax google-maps google-maps-api-3

有时此代码正常工作,但有时不更新位置和图标。 首先,它会根据用户的状态加载所有用户位置及其标记图标的地图,然后我使用时间间隔功能来更新用户的位置和状态。但是它并未更新用户的位置和标记图标map .it从ajax获得正确的响应。
请帮助我,已经检查了api凭证和启用的api

 var locations =["user","latitude","longitude","1",$order_id,$req_address,$dr_address,"2"];
var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 8,
  center: new google.maps.LatLng(current_lat, current_lon),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow({

});

var marker, i,name;

for (i = 0; i < locations.length; i++) {

     if(locations[i][3]==1)
     {
        //driver available
        dr_icon=Settings.base_url+'assets/map_icons/driver_available.png';
     }
     if(locations[i][3]==2)
     {
        //driver boocked
        dr_icon=Settings.base_url+'assets/map_icons/driver_boocked.png';
     }
     if(locations[i][3]==4)
     {
        //driver refilling
        dr_icon=Settings.base_url+'assets/map_icons/driver_refilling.png';
     }
     if(locations[i][3]==5)
     {
        //driver offline
        dr_icon=Settings.base_url+'assets/map_icons/driver_offline.png';
     }
  marker = new google.maps.Marker({
    icon:dr_icon,
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
        var order_id = locations[i][4];
        var req_address = locations[i][5];
        var name= locations[i][0];
        var dr_address=locations[i][6];
        var iwin='<div class="col">'+
        '<div class="card gradient-45deg-light-blue-cyan gradient-shadow">'+
        '<div class="card-content white-text"><span class="card-title">'+name+'</span>'+
          '<p>Order id : '+order_id+'<p></p> Address : '+req_address+
              '<p></p>Driver : '+dr_address+'</p>'+
        '</div><div class="card-action">'+
        '<a href="'+Settings.base_url+'index.php/Adminmap/trackCustomerDriver/'+locations[i][7]+'" class="waves-effect waves-light btn gradient-45deg-red-pink">Tracking</a>'+
        '</div> </div> </div>';

     infowindow.setContent(iwin);
     infowindow.open(map, marker);
    }
  })(marker, i));
}
/*change driver position*/   setInterval(function(){ 
    var myKeyVals = {country_id : $("#country").val(),state_id : $("#state").val(),city_id : $("#city").val(),service_area_id : $("#service_area").val(),driver_name : $("#driver_name").val()};

$.ajax({
        url: Settings.base_url+"index.php/Adminmap/driverLocationTracking",
        type: "POST",
        data: myKeyVals,
        success: function(result1){
        //console.log(result1);
            obj=JSON.parse(result1);
            console.log(obj);

            for (i = 0; i < obj.length; i++) { 
             /* marker = new google.maps.Marker({
                icon:'http://cdn.leafletjs.com/leaflet-0.6.4/images/marker-icon.png',
                position: new google.maps.LatLng(obj[i][1], obj[i][2]),
                map: map
              });

              google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                  infowindow.setContent(obj[i][0]);
                  infowindow.open(map, marker);
                }
              })(marker, i));*/
              if(obj[i][3]==1)
             {
                //driver available
                dr_icon=Settings.base_url+'assets/map_icons/driver_available.png';
             }
             if(obj[i][3]==2)
             {
                //driver boocked
                dr_icon=Settings.base_url+'assets/map_icons/driver_boocked.png';
             }
             if(obj[i][3]==4)
             {
                //driver boocked
                dr_icon=Settings.base_url+'assets/map_icons/driver_refilling.png';
             }
             if(obj[i][3]==5)
             {
                //driver offline
                dr_icon=Settings.base_url+'assets/map_icons/driver_offline.png';
             }
              var lat=parseFloat(obj[i][1]);
              var lon=parseFloat(obj[i][2]);

              latlong=new google.maps.LatLng(lat,lon)
              marker.setPosition(latlong);
              marker.setIcon(dr_icon);
            }
        }
    });
}, 2000);

0 个答案:

没有答案