2014-08-28 70 views
1

我在使用gmaps.js來顯示一個基本的谷歌地圖,在當前位置放置一個標記。我在變量中存儲經度和緯度座標,並使用它們來定義經度和緯度。緯度和經度變量從我的服務器不斷更新。發生這種情況時,地圖會在地圖上的新位置顯示標記,但整個地圖會刷新。我只想簡單地更新標記並保持地圖清爽。有沒有辦法做到這一點?如何在不刷新地圖的情況下更新gmaps中的標記?

<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

當然,保留對標記的引用,並更新其cordinates。 – Johan 2014-08-28 12:01:13

回答

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); 
相關問題