2009-08-04 67 views
1

對Javascript來說是全新的,但我需要它在我的項目中使用googlemaps,我試圖爲每個特定城市設置緯度,經度和地圖縮放值,所以從隱藏的表單輸入中獲取城市名稱,並使用Switch切換城市名稱。Javascript:使用切換爲函數設置變量值

cityDiv = document.getElementById('id_city'); 
cityDiv.value = idCity ; 

switch (idCity) 
{ 
case "city1": 
    var map_long = 31.37667; 
    var map_lat = 31.04306; 
    var map_zoom = 3; 
    break 
case "city2": 
    var map_long = 31.33333; 
    var map_lat = 29.85; 
    var map_zoom = 7; 
    break 
default: 
    var map_long = 31.37667; 
    var map_lat = 31.04306; 
    var map_zoom = 3; 
} 

function onLoad() { 
    map = new GMap(document.getElementById("map")); 
    map.addControl(new GSmallMapControl()); 
    map.addControl(new GMapTypeControl()); 
    map.centerAndZoom(new GLatLng(map_lat,map_long) , map_zoom); 

    GEvent.addListener(map, 'click', function(overlay, point) { 
     if (prev_pin) { 
      map.removeOverlay(prev_pin); 
      prev_pin = null; 
     } 
     //var yPoint = new YGeoPoint({{ place.latitude }},{{ place.longitude }}); 

     if (point) { 
      pin = new GMarker(point); 
      map.addOverlay(pin); 
      prev_pin = pin; 

      latDiv = document.getElementById('id_latitude'); 
      lngDiv = document.getElementById('id_longitude'); 
      lngDiv.value = point.x; 
      latDiv.value = point.y; 
     } 
    }); 

} 

對不起,這個新手問題。

此致敬禮。

從註釋編輯由geowa4:?!

的問題,問題是從來沒有設置變量:(,有啥錯我的代碼我改變「cityDiv.value = idCity;」到「變種idCity = cityDiv.value;「這沒有工作,但這次地圖拒絕加載

+0

問題是什麼? – 2009-08-04 19:27:57

+0

問題是? – 2009-08-04 19:28:22

+0

問題和問題是變量從未設置:(所以我的代碼有什麼問題?! 我將「cityDiv.value = idCity; 」更改爲「var idCity = cityDiv.value;」 工作,但這次地圖拒絕加載 – Hamza 2009-08-04 19:49:35

回答

1

您的問題是範圍,純粹和簡單。使用var將您的變量作用於當前的詞法作用域,在var map_long = 31.37667;等的情況下是switch語句。一旦你超出了範圍,那麼在該範圍內聲明的任何變量消失,除非保持外部參考。您可以刪除var語句,但這會使變量成爲全局變量。我會建議在一個對你有意義的範圍內聲明你的變量,只有在絕對必要時才使它們成爲全局變量。

2

您是否嘗試過傾銷您的值,逐步調試?Alert statementsconsole.log()聲明,以查看頁面認爲該值的選擇是?

你也可以考慮做一個對象,你c然後更新:

// Untested, but you should get the gist 
var cityInfo = { 
    map_long : 0.0, 
    map_lat : 0.0, 
    map_zoom : 0 
}; 

switch (idCity) { 
case "city1": 
    cityInfo.map_long = 31.37667; 
    cityInfo.map_lat = 31.04306; 
    cityInfo.map_zoom = 3; 
    break 
case "city2": 
    cityInfo.map_long = 31.33333; 
    cityInfo.map_lat = 29.85; 
    cityInfo.map_zoom = 7; 
    break 
default: 
    cityInfo.map_long = 31.37667; 
    cityInfo.map_lat = 31.04306; 
    cityInfo.map_zoom = 3; 
} 
3

嘗試下面的代碼:

mapInfo = 
{ 
    "city1": 
    { 
    "long": 31.37667, 
    "lat": 31.04306, 
    "zoom": 3 
    }, 
    "city2": 
    { 
    "long": 31.33333, 
    "lat": 29.85, 
    "zoom": 7 
    } 
}; 

function onLoad() { 
    map = new GMap(document.getElementById("map")); 
    map.addControl(new GSmallMapControl()); 
    map.addControl(new GMapTypeControl()); 
    var cityDiv = document.getElementById('id_city'); 
    var idCity = cityDiv.value || "city1"; 
    map.centerAndZoom(new GLatLng(mapInfo[idCity].lat,mapInfo[idCity].long) , mapInfo[idCity].zoom); 

    GEvent.addListener(map, 'click', function(overlay, point) { 
     if (prev_pin) { 
       map.removeOverlay(prev_pin); 
       prev_pin = null; 
     } 
     //var yPoint = new YGeoPoint({{ place.latitude }},{{ place.longitude }}); 

     if (point) { 
       pin = new GMarker(point); 
       map.addOverlay(pin); 
       prev_pin = pin; 

       latDiv = document.getElementById('id_latitude'); 
       lngDiv = document.getElementById('id_longitude'); 
       lngDiv.value = point.x; 
       latDiv.value = point.y; 
     } 
    }); 

}