我正在使用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>
答案 0 :(得分: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);