【问题标题】:How can I update markers in gmaps without refreshing the map?如何在不刷新地图的情况下更新地图中的标记?
【发布时间】:2014-10-22 06:41:30
【问题描述】:

我正在使用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>

【问题讨论】:

  • 当然,保留对标记的引用并更新其坐标。

标签: javascript


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2019-05-24
    • 2019-10-08
    • 2017-03-22
    • 2020-08-17
    相关资源
    最近更新 更多