如何在不刷新地图的情况下更新gmaps中的标记?

3
我正在使用gmaps.js来显示基本的谷歌地图,并在当前位置放置一个标记。我将纬度和经度坐标存储在变量中,并使用它们来定义经度和纬度。这些变量会不断从服务器更新。当发生这种情况时,地图将在新位置上显示标记,但整个地图都会刷新。我希望只更新标记,并保持地图不刷新。有什么办法可以做到这一点吗?
<script> 
var map;
var Latitude = //this is being updated from my server constantly
var Longitude = //this is being updated from my server constantly
$(document).ready(function(){
  map = new GMaps({
    el: '#map',
    lat: Latitude,
    lng: Longitude
  });
  map.addMarker({
    lat: Latitude,
    lng: Longitude,
    title: 'Lima',
    details: {
      database_id: 42,
      author: 'HPNeo'
    },
    click: function(e){
      if(console.log)
        console.log(e);
      alert('You clicked in this marker');
    },
    mouseover: function(e){
      if(console.log)
        console.log(e);
    }
  });
  map.addMarker({
    lat: -12.042,
    lng: -77.028333,
    title: 'Marker with InfoWindow',
    infoWindow: {
      content: '<p>HTML Content</p>'
    }
  });
});
</script>

1
当然,保留标记的引用并更新其坐标。 - Johan
1个回答

1
// 1. Create a marker and keep a reference to it:

var latLng = new google.maps.LatLng(-12.042, -77.028333),
    marker = new google.maps.Marker({ position: latLng , map: map });

marker.setMap(map);

// 2. Move it around by setting a new position:

var newPosition = new google.maps.LatLng(-12.042, -78.028333);
marker.setPosition(newPosition);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接