2010-11-24 61 views
4

我在'刷新'我的Google地圖後遇到問題,我無法單獨放置標記(單擊)。但在刷新我的地圖(初始化之前)之前,我可以通過單擊來放置標記。我可以知道代碼有什麼問題嗎?刷新Google地圖阻止添加標記的能力

下面是我的代碼...

//Initialize the map 
function initialize() { 
    var myLatlng = new google.maps.LatLng(2,110); 
    var myOptions = { 
     zoom: 3, 
     center: myLatlng, 
     mapTypeId: google.maps.MapTypeId.HYBRID 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    infowindow = new google.maps.InfoWindow({ 
     content: "loading..." 
    }); 
} 

// Listen for click for markers 
function marker() 
{ 
    google.maps.event.addListener(map, 'click', function(event) { 
     addMarker(event.latLng); 
    }); 
} 

// Place markers in by click 

function addMarker(location) { 
    marker = new google.maps.Marker({ 
     position: location, 
     map: map, 
     title:"Specified Location", 
     icon: 'images/greenPoint.png' 
    }); 
    markersArray.push(marker); 
} 


function refreshMap() 
{ 
    var myLatlng = new google.maps.LatLng(1.1,107); 
    var myOptions = { 
     zoom: 4, 
     center: myLatlng, 
     mapTypeId: google.maps.MapTypeId.HYBRID 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
    myOptions); 
} 

回答

0

如果我理解你的問題正確的話,你說你叫refreshMap功能後,您的地圖不工作。聽起來像一個範圍界定問題,其中map變量第二次處於不同的範圍。嘗試把這一行:

var map = null; 

在文件的最頂端,以確保所有的map引用的是同一個全局變量map

+0

嗨科林,但我已經把變種地圖;作爲一個全球變量。 – 2010-11-25 01:08:30

1

爲什麼要首先創建一個新的google.maps.Map對象?你應該做這樣的事情,而不是:

function refreshMap() 
{ 
    var myLatlng = new google.maps.LatLng(1.1,107); 
    var myOptions = { 
     zoom: 4, 
     center: myLatlng, 
    }; 
    map.setOptions(myOptions); 
} 
+0

嗨,Eric,或許我沒有讓我的問題聽起來更清晰。例如,在我初始化我的地圖後,我放下了一些標記。之後,我按下refreshMap()按鈕(這將允許我清除所有標記),然後再次按下marker()函數後,我將能夠再次放下標記...因爲我做了什麼現在,刷新我的地圖後,當我按下功能添加標記後,我無法添加任何東西。謝謝 – 2010-11-25 01:12:54