2011-11-30 273 views
0

,我有以下的Javascript,我修改了標準的谷歌地圖API初始化()函數和標準addMarker功能。地圖加載和居中,但標記不會被添加到地圖中。我看着previous question,但我不知道我做錯了..添加標記 - 谷歌地圖API V3

<script type="text/javascript"> 
// Note that using Google Gears requires loading the Javascript 
// at http://code.google.com/apis/gears/gears_init.js 

var siberia = new google.maps.LatLng(60, 105); 
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 
var usa = new google.maps.LatLng(44.58, -95.46); //middle of the US w 50 states  

var browserSupportFlag = new Boolean(); 

function initialize() { 
    var myOptions = { 
    zoom: 4, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions) 
    map.setCenter(usa); 
} 

function getMyLocation() { 
    var initialLocation= newyork; 
    var myOptions = { 
    zoom: 10, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    // Try W3C Geolocation (Preferred) 
    if(navigator.geolocation) { 
     browserSupportFlag = true; 
     navigator.geolocation.getCurrentPosition(function(position) { 
     initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 
     map.setCenter(initialLocation); 
     addMarker(initialLocation); 
    }, function() { 
    handleNoGeolocation(browserSupportFlag); 
    }); 
    // Try Google Gears Geolocation 
    } else if (google.gears) { 
     browserSupportFlag = true; 
     var geo = google.gears.factory.create('beta.geolocation'); 
     geo.getCurrentPosition(function(position) { 
     initialLocation = new google.maps.LatLng(position.latitude,position.longitude); 
     map.setCenter(initialLocation); 
     addMarker(initialLocation); 
    }, function() { 
     handleNoGeoLocation(browserSupportFlag); 
    }); 
    // Browser doesn't support Geolocation 
    } else { 
     browserSupportFlag = false; 
     handleNoGeolocation(browserSupportFlag); 
    } 

    function handleNoGeolocation(errorFlag) { 
     if (errorFlag == true) { 
      alert("Geolocation service failed."); 
      initialLocation = newyork; 
     } else { 
      alert("Your browser doesn't support geolocation. We've placed you in Siberia."); 
      initialLocation = siberia; 
     } 
     map.setCenter(usa); 
    } 

} 

function addMarker(location) { 
    marker = new google.maps.Marker({ 
    position: location, 
    map: map, 
    title: "Hello World!" 
    }); 
    //markersArray.push(marker); 
} 
</script> 
+0

你並沒有在任何地方調用getLocation函數,因此爲什麼addMarker也沒有被調用... – Flukey

+0

getLocation函數在我按下按鈕時被調用.. JK0124

+0

好的,那麼在這種情況下,如果你發佈了整個代碼,它會有所幫助。 – Flukey

回答

0

所以你的問題是與範圍有關:

添加這上面你用來初始化函數:

var map; 

而在你的初始化函數改變這一點:

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions) 

這樣:

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions) 

你並不需要在你的getLocation功能來重新創建地圖:

所以刪除這些行:

var myOptions = { 
    zoom: 10, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

這裏是與變化的jsfiddle:

http://jsfiddle.net/bQ3AG/

編輯:我剛剛注意到,當你指向他們的位置時,你wa NT縮放級別設置爲10

要做到這一點,你可以使用:map.setZoom(10);,而不是通過新的選項。