最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to move marker smoothly without blinking in map - Stack Overflow

programmeradmin1浏览0评论

I have problem in my markers every time I get request to our server to get the newly location of my devices and updated the position of my markers on the map,when my vehicle devices move,the marker will jump to the newly location and its flickering. How can I avoid this not to flickering,or my Marker can move smoothly.

Thank you in advance.

var map;
var marker;
var markerarray =[];

setInterval(function(){

    $.ajax({
        type: "post",
        url: "vehiclecordinates.php",
        success: function(data){

            coordinates = data.latlng;
            vehiclename = data.vehiclename;

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

                newcoordinate = new google.maps.LatLng(coordinates[i].split(",")[0],coordinates[i].split(",")[1]);
                marker =  new MarkerWithLabel({
                     map:map,
                     labelClass: "mylabels",
                     labelStyle: {opacity: 1.0},
                     labelContent: '<div>'+ vehiclename[i]+'</div>',
                     icon:{
                         //some options here
                     },
                });

                marker.setPosition(newcoordinate);
                markerarray.push(marker);
            }
        }
    });

    setTimeout(function () {
        removeMarkers();
        delete marker;
    }, 1000);
},5000);

function removeMarkers() {
    for(var i = 0; i < markerarray.length; i++) {
        markerarray[i].setMap(null);
    }
}

I have problem in my markers every time I get request to our server to get the newly location of my devices and updated the position of my markers on the map,when my vehicle devices move,the marker will jump to the newly location and its flickering. How can I avoid this not to flickering,or my Marker can move smoothly.

Thank you in advance.

var map;
var marker;
var markerarray =[];

setInterval(function(){

    $.ajax({
        type: "post",
        url: "vehiclecordinates.php",
        success: function(data){

            coordinates = data.latlng;
            vehiclename = data.vehiclename;

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

                newcoordinate = new google.maps.LatLng(coordinates[i].split(",")[0],coordinates[i].split(",")[1]);
                marker =  new MarkerWithLabel({
                     map:map,
                     labelClass: "mylabels",
                     labelStyle: {opacity: 1.0},
                     labelContent: '<div>'+ vehiclename[i]+'</div>',
                     icon:{
                         //some options here
                     },
                });

                marker.setPosition(newcoordinate);
                markerarray.push(marker);
            }
        }
    });

    setTimeout(function () {
        removeMarkers();
        delete marker;
    }, 1000);
},5000);

function removeMarkers() {
    for(var i = 0; i < markerarray.length; i++) {
        markerarray[i].setMap(null);
    }
}
Share Improve this question edited Nov 11, 2016 at 9:15 vabada 1,8145 gold badges30 silver badges38 bronze badges asked Aug 27, 2014 at 18:31 jemzjemz 5,15319 gold badges69 silver badges108 bronze badges 2
  • If you don't want them to flicker, don't "remove" them from the map, update their position. – geocodezip Commented Aug 27, 2014 at 18:39
  • but if I will not remove the marker my browser will hang,. – jemz Commented Aug 27, 2014 at 18:44
Add a ment  | 

1 Answer 1

Reset to default 7

If you don't want them to flicker, don't "remove" them from the map, update their position.

var map;
var marker;
var markerarray =[];

setInterval(function(){
  $.ajax({
    type: "post",
    url: "vehiclecordinates.php",
    success: function(data){
      coordinates = data.latlng;
      vehiclename = data.vehiclename;
      for (var i = 0; i < coordinates.length; i++) {
        newcoordinate = new google.maps.LatLng(coordinates[i].split(",")[0],coordinates[i].split(",")[1]);
        if (markerarray[vehiclename[i]] && markerarray[vehiclename[i]].setPosition){
          markerarray[vehiclename[i]].setPosition(newcoordinate);
        else {
          marker =  new MarkerWithLabel({
            map:map,
            labelClass: "mylabels",
            labelStyle: {opacity: 1.0},
            labelContent: '<div>'+ vehiclename[i]+'</div>',
            icon:{
              //some options here
            }
          });
          marker.setPosition(newcoordinate);
          markerarray[vehiclename[i]] = marker;
        }
      }
    }
  });
},5000);

See example based off the example in this similar question

发布评论

评论列表(0)

  1. 暂无评论