哪些标记在地图上无法正确移动

时间:2014-12-03 12:36:32

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

JSON数据样本(来自评论):

[{"id":"280","id_vehicle":"VL0847810531","lat":"30.0761","longi":"1.01981","spee‌​d":"144","time":"2014-12-03 12:07:23"},{"id":"202","id_vehicle":"VL0645210631","lat":"34.7344","longi":"7.32‌​019","speed":"78","time":"2014-12-03 11:55:44"}] 

function updateLocations(jsonData)
{
            for (i=0 ;i< jsonData.length; i++) //for all vehicles
            {
                var id_vehicle = jsonData[i]["id_vehicle"]; 
                var lat =        jsonData[i]["lat"];
                var lng =      jsonData[i]["longi"];
                var speed =      jsonData[i]["speed"];
                var str_time =   jsonData[i]["time"];

                /************************update list*******************************/
                var state_icon, marker_icon, state;                    
                var time = moment(str_time);                                        
                var last_10_Min = moment().subtract({minutes: 60 + 10});
                if(time.isBefore(last_10_Min)) //if before 10 last minutes
                {
                    state_icon = INACTIVE_IMG;
                    marker_icon = INACTIVE_VEHICLE;
                    state = "INACTIVE";
                } 
                else //if befor
                {
                    if(jsonData[i]["speed"] > 10) //if > 2 km/h then running
                    {
                        state_icon = RUN_IMG;
                        marker_icon = RUN_VEHICLE;
                        state = "RUN";
                    }
                    else
                    {
                        state_icon = STOP_IMG;
                        marker_icon = STOP_VEHICLE;
                        state = "STOP";
                    }
                }    
                $("#state_img_"+id_vehicle).attr("src", state_icon);   
                $("#state_img_"+id_vehicle).attr('state',state);

                $("#select_"+id_vehicle).attr("disabled" , false ); // enable selection

                /************************update location info*******************************/
                var locationInfo = new Array();                
                img = "<img src=" + state_icon + " width='16' height='16' >";
                locationInfo.push("Etat : " + state + " " +  img + "<br>");        
                locationInfo.push("Latitude : " +  lat + "<br>");
                locationInfo.push("Longitude : " +  lng + "<br>");            
                locationInfo.push("Vitess: " +  speed + " klm/h<br>");
                locationInfo.push("Temps : " +  str_time + "<br>");            
                $("#info_location_" +id_vehicle).html(locationInfo.join(""));


                /*****************update vehicles on map *************/   
                try {
                    cBox = $("#select_"+id_vehicle);                        
                    if(cBox.is(':checked')) //update selected only
                    {
                        //get marker index
                        var id_map = cBox.attr("id_map");
                        //change title
                        title = "Latitude: "+ lat + "\nLongitude: " + lng + "\nSpeed: " + speed + "\nTime: " + str_time;   
                        arrayMarker[id_map].setTitle(title); //update title                   
                        arrayMarker[id_map].setIcon(marker_icon);

                        //move marker
                        arrayMarker[id_map].setPosition( new google.maps.LatLng(parseFloat(lat),parseFloat(lng)) ); 
                   }
                }catch(error){};
            }                
}
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////

我的问题是为什么这个功能被执行(更新位置)只是地图上的fisrt车辆正确移动,ohers更新(标题,图标......)但不移动?

我注意到,他们快速移动并返回旧位置。

感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

最后我发现了问题,就在这里:

var marker = new MarkerWithLabel({......});

arrayMarker [id_map] = marker; //将markerMarker中的marker放在indexMarker位置

当我使用MarkerWithLabel填充我的arrayMarker时出现错误(第3个库)

更改为原生google.maps.Marker它正常工作:

var marker = new google.maps.Marker({......}); arrayMarker [id_map] = marker;