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>
你並沒有在任何地方調用getLocation函數,因此爲什麼addMarker也沒有被調用... – Flukey
getLocation函數在我按下按鈕時被調用.. – JK0124
好的,那麼在這種情況下,如果你發佈了整個代碼,它會有所幫助。 – Flukey