2009-11-26 93 views
0

我開發了一個簡單的Google地圖應用程序。我只需要在地圖上放置一個標記。無論出於何種原因,標記都放置在地圖可見區域之外。這有點奇怪,因爲地圖以標記的座標爲中心。這裏是代碼:Google地圖居中或標記定位問題

var point1 = new GLatLng(location1.lat,location1.lon);    
    map1.setCenter(point1, 15); 
var marker1 = new GMarker(point1); 
map1.addOverlay(marker1); 
map1.setCenter(point1); 

當我們拖動地圖一點點,我們可以看到標記。我需要的是以地圖拖動方式顯示標記的方式居中。

任何人都可以幫助我嗎?

+0

當包含地圖的div與GMap2的大小不一樣時,我發生了這種情況,因爲在頁面的onLoad中重新縮放。 – 2009-11-26 10:51:19

+0

是否有可能location1.lat和location1.lon是String對象,而不是數字? – 2009-11-26 11:03:07

回答

1

我相信GLatLng對象將接受字符串參數,以及 - 但爲了安全起見我會確保他們都是整數 - 嘗試使用:

new GLatLng(parseInt(location.lat), parseInt(location.lon)); 

我也注意到你叫map.setCenter第二時間,這應該沒有必要。

使用下面的代碼真的應該如果你依然需要我會檢查「位置」對象,以確保.lat和.lon值是否被正確填充問題做

map=new GMap(document.getElementById("map")); 
    var point = new GLatLng(parseInt(location.lat), parseInt(location.lon)); 
    map.setCenter(point,5); 
    var marker = new GMarker(point); 
    map.addOverlay(marker); 

0

檢查這個代碼了:

var map = new GMap(document.getElementById("map")); 
/* -- snip -- */ 
map.centerAndZoom(new GPoint(-1.2736, 53.0705), 8); 

從一個網站,我前一陣子做。隨時檢查來源:

http://www.primrose-house.co.uk/localattractions

只需點擊右上角的鏈接切換到地圖視圖。