2014-01-26 27 views
0

我有HTML页面,其中包含Google地图。我有定时器,哪个调用函数,哪个移动地图。地图移动但每次都闪烁。那么如何解决这个错误?设置坐标中的Google地图闪烁

function Replace(lt,ln) 
{ 
var location = new google.maps.LatLng(lt, ln); 
    var myOptions = { 
     zoom: 15, 
     center: location, 
     mapTypeControl: true, 
     mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }, 
     navigationControl: true, 
     navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    var companyPos = new google.maps.LatLng(lt, ln); 
    var companyMarker = new google.maps.Marker({ 
     position: companyPos, 
     map: map, 
     title: "Test" 
    }); 

    } 

回答

0

这是不是一个错误,你创建一个新的地图实例每次运行该功能(什么导致闪烁)时间。

独立的代码,在第一次运行创建地图/标记 - 例如,以后只更新地图中心和标记位置:

function Replace(lt,ln){ 

    var location = new google.maps.LatLng(lt, ln); 
    var companyPos = new google.maps.LatLng(lt, ln); 
    //map not created yet 
    if(!window.map){ 
      var myOptions = { 
      zoom: 3, 
      center: location 
      }; 
      //Note: map and companyMarker must be global 
      map = new google.maps.Map(document.getElementById("map_canvas"), 
            myOptions); 
      companyMarker = new google.maps.Marker({ 
      position: companyPos, 
      map: map, 
      title: "Test" 
      }); 
    } 
    //map already initialized, update center and position 
    else{ 
     map.setCenter(location); 
     companyMarker.setPosition(companyPos); 
    } 
}